Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6653317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:15:16+00:00 2026-05-26T01:15:16+00:00

Lets say i have an array in PHP $test[‘michael_unique_id’] = 3; $test[‘john_unique_id’] = 8;

  • 0

Lets say i have an array in PHP

$test['michael_unique_id'] = 3;
$test['john_unique_id'] = 8;
$test['mary_unique_id'] = 10;
.
.
.
.
$test['jimmy_unique_id'] = 4;

(the values (3,8,10………4) are unique)

Lets say i want to search for the unique id 10, and get the order of the matching element in the array. In this example, the third element has the value 10, so i should get the number 3.

I can do it by scanning with a for loop looking for the value i’m searching and then get the $i value when i have a match, but i wonder if there is any built-in function (or a better method) that implements this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:15:17+00:00Added an answer on May 26, 2026 at 1:15 am

    You can get all of the array’s values as an array with array_values() and then use array_search() to get the position (offset) of that value in the array.

    $uniq_id  = 10;
    $all_vals = array_values($test);  // => array(3, 8, 10, ... )
    echo array_search( $uniq_id, $all_vals ); // => 2
    

    Because PHP array indices are zero-based, you’ll get 0 for the first item, 1 for the second item, etc. If you want the first item to be “1,” then just add one. All together now:

    $uniq_id = 10;
    
    echo array_search( $uniq_id, array_values( $test ) ) + 1; // => 3
    

    It’s not clear to me, however, that this is necessarily as performant as just doing a foreach:

    $uniq_id  = 10;
    $idx      = 1;
    
    foreach($test as $val) {
      if($val == $uniq_id) {
        break;
      }
    
      $idx++;
    }
    
    echo $idx; // => 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have an array numbers that contains the following values: int numbers
Lets say I have an array of length n and I want to pick
is there something like PHP: max() in javascript? lets say i have an array
Lets say that I have an array that I want to convert to a
Lets say I have an array like this: string [] Filelist = ... I
Lets say I have this array, int[] numbers = {1, 3, 4, 9, 2};
Lets say I have an array which has n dimensions. Now in order to
Lets say I have an array with a structure like this: $arr= Array( array(
Lets say I have an array : a=[[1,2,3],[4,5]] and I have another array :
Lets say I have the following array in JavaScript: var skins = new Array('Light',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.