Tag Archives: array key

PHP: Identify the Offset of a Specific Array Element CodeUnit 04 DEC 2012

To learn the offset or array key for a specific array element in a given array turns out to be fairly easy, thanks to the extra search parameter that the always useful array_keys (return all the keys or a subset of the keys of an array) function offers us.

From the manual description: array array_keys ( array $input [, mixed $search_value = NULL [, bool $strict = false ]] ) – array_keys() returns the keys, numeric and string, from the input array. If the optional search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the input are returned.

Knowing this, we can now find exactly where a specific element sits by simply entering a search value when calling the array_keys function, which in practice would look something like this:

$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
/*
Array
(
    [0] => 0
    [1] => 3
    [2] => 4
)
*/

The above example shows us that the array element “blue” appears at offset 0, 3 and 4 in the source array.

Nifty.

Related Link: http://www.php.net/manual/en/function.array-keys.php

PHP: Compare the Keys of an Array and find the Difference Programming 27 FEB 2012

To compare two arrays to see if there is a difference, one often makes use of the array_diff function which takes two arrays, compares array1 against array2 and then returns the difference.

Now if you actually want to check for a difference in array keys (say for example you’ve built up two associative arrays of table columns and want to check one table’s structure versus another), you can make use of the array_diff_key function, which acts in a very similar fashion to array_diff except for the part where it compares the keys of the associative input arrays as opposed to the values contained within the arrays.

In practice:

$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

print_r(array_diff_key($array1, $array2));

/* Results in:
array(2) { 
  ["red"]=>int(2)
  ["purple"]=>int(4)
}
*/

Nifty.

PHP: How to Get the Associated Key from an Array given a Value Programming 06 FEB 2012

If you have a value and you suspect that it might be contained within your array, can you somehow find the key linked to that value if it does exist?

The answer is yes, thanks to the handy array_search function which searches an array for a given value and returns the corresponding key if the search is successful, and false if the value isn’t found in the array.

In practice:

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

(Note that similar to strpos validations, you need to make use of the === operator when testing the return value of this function).

The array_search returns the first match it comes across, meaning that if the value is in the array, you’ll only know of one instance. If it is important to know all of the corresponding keys where the value can be found, rather use the array_keys function, but this time with the optional search parameter.

In practice:

$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
/*
returns:
Array
(
    [0] => 0
    [1] => 3
    [2] => 4
)
*/

Useful.