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.
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 o ...
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 $i ...
Coming from PHP, I'm well versed in using the handy explode function to force a string into an array, using a specified delimiter character to chop up the string into the little bits that are to be stored in the array. Happily for me, plain old vanilla JavaScript also has this ability built into the language, ch ...
Coming from PHP, I'm well versed in using the handy implode function to flatten an array into a string, specifying the delimiter character to be used to indicate the border between the flattened array elements. Happily for me, plain old vanilla JavaScript also has this ability built into the language, choosing t ...
PHP's implode function is extremely useful for flattening a one dimensional array into a string. This time around, we want to create a flat string of an array's keys, ignoring the actual values for a change. To achieve this is actually very simple - it's just a matter of combining the stock standard array_keys() ...
Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under whichever martial arts dojo is closest at the time, and for the most part, simply enjoys life with his amazing wife and daughter.
Oh, and he draws ever now and then too.
This is a collection of things that he has managed to find the time to scribble down since 2007.
Looking for Something?
Jump to Category: