I am surprised that many answers, including voted answers, point out as a solution a coded code, that filter the matrix to a single occurrence, manually coded in a mere conditional.
Well... My suggestion is similar to that given by the friend @Cahe, but she much leaner and kills the problem with sniper precision and, breaking, still avails of more modern language features: Iterators.
It was not a solution created by me, hence the documentation in English, but I kept the credits of the author, including the link from where the snippet was removed. But unfortunately, either the same was removed from the manual, or deleted during the redesign of the site.
/**
* Searches value inside a multidimensional array, returning its index
*
* Original function by "giulio provasi" (link below)
*
* @param mixed|array $haystack
* The haystack to search
*
* @param mixed $needle
* The needle we are looking for
*
* @param mixed|optional $index
* Allow to define a specific index where the data will be searched
*
* @return integer|string
* If given needle can be found in given haystack, its index will
* be returned. Otherwise, -1 will
*
* @see http://www.php.net/manual/en/function.array-search.php#97645
*/
function search( $haystack, $needle, $index = NULL ) {
if( is_null( $haystack ) ) {
return -1;
}
$arrayIterator = new \RecursiveArrayIterator( $haystack );
$iterator = new \RecursiveIteratorIterator( $arrayIterator );
while( $iterator -> valid() ) {
if( ( ( isset( $index ) and ( $iterator -> key() == $index ) ) or
( ! isset( $index ) ) ) and ( $iterator -> current() == $needle ) ) {
return $arrayIterator -> key();
}
$iterator -> next();
}
return -1;
}
To use, of course, just invoke the function by passing the array to be searched in the first argument and what is searched in the second.
var_dump( search( $valores, '5745,5744,7' ) ); // Saída int(2)
Optionally we have the third argument that allows you to restrict the search to a specific matrix index.
In the use example immediately above would be:
var_dump( search( $valores, '5745,5744,7', 'fk' ) );
It is not a performance gain because all logic resides in a single IF, but it is an alternative that deserves to be taken into account.
The function returns an integer that must be used to access the indexes of the matrix:
var_dump( $valores[ search( $valores, '5745,5744,7', 'fk' ) ] );
Note only that this didactic example takes into account a real and existing value. In an actual application where the searched value may not exist, a check must be made if the function returns -1, in view of the fact that, -1 is not a valid index for a matrix, even if it is, improperly, valid for PHP.
Perfect guy, it was exactly what I needed, without a loop to go through the whole array and extremely fast...
– Kenny Rafael
It loops, but by using Iterators, mainly a recursive, the task is much better performed than invoking the function itself again, again and again to each new array nested to the structure.
– Bruno Augusto
I got it. Thank God!
– Kenny Rafael