PHP: How to find out the position of a value in an array?

Asked
Active3 hr before
Viewed126 times

8 Answers

position
90%

The problem is that you don't have a numerical index on your array. Using array_values() will create a zero indexed array that you can then search using array_search() bypassing the need to use a for loop.,Other folks have suggested array_search() which gives the key of the array element where the value is found. You can ensure that the array keys are contiguous integers by using array_values():,You could loop over the array manually and find the index but why do it when there's a function for that. This function always returns a key and it will work well with associative and normal arrays., sorry, i didn't specify the type of array. The array i use is a multi-dimensional array please see my answer for the solution – Aakash Chakravarthy Jun 3 '10 at 4:38

From the docs:

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
load more v
88%

You want to know if an element is in an array, and, if it is, you want to know where it is located. ,If a value is in the array multiple times, array_search() is only guaranteed to return one of the instances, not the first instance. , Use the !== check against false because if your string is found in the array at position 0, the if evaluates to a logical false, which isn’t what is meant or wanted. ,Take O’Reilly with you and learn anywhere, anytime on your phone and tablet.

$position = array_search($value, $array);
if ($position !== false) {
   // the element in position $position has $value as its value in array $array
}
load more v
72%

array_values() - Return all the values of an array, Returns the key for needle if it is found in the array, false otherwise. , If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead. ,in_array() - Checks if a value exists in an array

load more v
65%

$people = array(
   2 => array(
      'name' => 'John',
      'fav_color' => 'green'
   ),
   5 => array(
      'name' => 'Samuel',
      'fav_color' => 'blue'
   ));
$found_key = array_search('blue', array_column($people, 'fav_color'));
load more v
75%

Iterate associative array using foreach loop in PHP,How to delete an array element based on key in PHP?,How to loop through an associative array and get the key in PHP?,How to access an associative array by integer index in PHP?

Syntax:

array array_keys($input_array, $search_value, $strict)
load more v
40%

The array_search() function searches an array for a given value and returns the key. The function returns the key for val if it is found in the array. It returns FALSE if it is not found. If val is found in the array arr more than once, then the first matching key is returned.,The array_search() function returns the key for val if it is found in the array. It returns FALSE if it is not found. If val is found in the array arr more than once, then the first matching key is returned.,strict − Possible values are TRUE or FALSE. Search for identical elements in the array, set to TRUE.,arr − The array to be searched

Syntax

array_search(val, arr, strict)
load more v
22%

The PHP array_search() is an inbuilt function that is widely used to search and locate a specific value in the given array. If it successfully finds the specific value, it returns its corresponding key value. If the element is found twice or more, then the first occurrence of the matching value's key will be returned.,The array_search() function returns the corresponding element's key that is passed.,$value (required):This parameter represents the value that the user wishes to search in the given array.,Example 3: In the below program, we learn how to search for an array value with the help of array_search() function if the strict_parameter is set to TRUE.

Dilip is at position 1
load more v
60%

If you want to access an individual value form an indexed, associative or multidimensional array you can either do it through using the array index or key.,Is this website helpful to you? Please give us a like, or share your feedback to help us improve. Connect with us on Facebook and Twitter for the latest updates.,How to print or echo all the values of an array in PHP,How to remove duplicate values from an array in PHP

<?php
// Indexed array
$sports = array("Baseball", "Cricket", "Football", "Shooting");
 
// Associative array
$cities = array("France"=>"Paris", "India"=>"Mumbai", "UK"=>"London", "USA"=>"New York");
 
// Multidimensional array
$superheroes = array(
    array(
        "name" => "Peter Parker",
        "character" => "Spider-Man",
    ),
    array(
        "name" => "Tony Stark",
        "character" => "Iron-Man",
    ),
    array(
        "name" => "Clark Kent",
        "character" => "Super-Man",
    )
);
 
echo $sports[0]; // Outputs: Baseball
echo "<br>";
echo $sports[1]; // Outputs: Cricket
echo "<br>";
echo $cities["France"]; // Outputs: Paris
echo "<br>";
echo $cities["USA"]; // Outputs: New York
echo "<br>";
echo $superheroes[0]["name"]; // Outputs: Peter Parker
echo "<br>";
echo $superheroes[1]["character"]; // Outputs: Iron-Man
?>

Other "position-undefined" queries related to "PHP: How to find out the position of a value in an array?"