Search for values in a nested array
I have an array as follows
array(2) { ["operator"] => array(2) { ["qty"] => int(2) ["id"] => int(251) } ["accessory209"] => array(2) { ["qty"] => int(1) ["id"] => int(209) } ["accessory211"] => array(2) { ["qty"] => int(1) ["id"] => int(211) } } I am trying to find a way to check the id value in an array and return bool. I am trying to find a quick way that does not require creating a loop. Using the in_array function did not work, and I also read that it is rather slow.
In the php reference, someone recommended using flip_array () and then isset (), but I can't get it to work for a 2-dimensional array.
do something like
if($array['accessory']['id'] == 211) will also work for me, but I need to match all the keys containing the accessory - not sure how to do this
Anyway, I spin in circles and can use some help. It seems to be easy. Thanks.
Hi dardub, you can use array_walk to check if a particular value is inside your array. array_walk iterates through all the elements of your array and applies the function provided to them, so you basically need to create this function. It is used as follows:
$arr = array( 'one' => array('id' => 1), 'two' => array('id' => 2), 'three' => array('id' => 3) ); function checkValue($value, $key) { echo $value['id']; } array_walk($arr, 'checkValue'); You just need to add any conventions / validations that you need to your function.
Hope this helps.
M.
EDIT: PHP docs on array_walk http://www.php.net/manual/en/function.array-walk.php
<?php //PHP 5.3 way to do it function searchNestedArray(array $array, $search, $mode = 'value') { foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $key => $value) { if ($search === ${${"mode"}}) return true; } return false; } $data = array( array('abc', 'ddd'), 'ccc', 'bbb', array('aaa', array('yyy', 'mp' => 555)) ); var_dump(searchNestedArray($data, 555)); I used the static method because I need it in the class, but if you want, you can use it as a simple function.
/** * Given an array like this * array( * 'id' => "first", * 'title' => "First Toolbar", * 'class' => "col-md-12", * 'items' => array( * array( * 'tipo' => "clientLink", * 'id' => "clientLinkInsert" * ) * ) * ) * * and array search like this * array("items", 0, "id") * * Find the path described by the array search, in the original array * * @param array $array source array * @param array $search the path to the item es. ["items", 0, "tipo"] * @param null|mixed $defaultIfNotFound the default value if the value is not found * * @return mixed */ public static function getNestedKey($array, $search, $defaultIfNotFound = null) { if( count($search) == 0 ) return $defaultIfNotFound; else{ $firstElementSearch = self::array_kshift($search); if (array_key_exists($firstElementSearch, $array)) { if( count($search) == 0 ) return $array[$firstElementSearch]; else return self::getNestedKey($array[$firstElementSearch], $search, $defaultIfNotFound); }else{ return $defaultIfNotFound; } } }