php - Find a value & key in a multidimensional array -
this question has answer here:
i have 2 arrays, need find if 1 of values in array 1 matches 1 of values in array two, multi-dimensional array. need check value array 1 in specific key in array two, "principal" key "authority" key may hold value.
here array one:
array ( [0] => 17 [1] => 6 [2] => 3 [3] => 2 )
and array 2 [actually truncated readability]:
array ( [modaccessresourcegroup] => array ( [3] => array ( [0] => array ( [principal] => 0 [authority] => 9999 [policy] => array ( [load] => 1 ) ) [1] => array ( [principal] => 2 [authority] => 10 [policy] => array ( [add_children] => 1 [create] => 1 [copy] => 1 [delete] => 1 [list] => 1 [load] => 1 [move] => 1 [publish] => 1 [remove] => 1 [save] => 1 [steal_lock] => 1 [undelete] => 1 [unpublish] => 1 [view] => 1 ) ) .... truncated .... [13] => array ( [principal] => 16 [authority] => 9999 [policy] => array ( [load] => 1 ) ) ) [8] => array ( [0] => array ( [principal] => 0 [authority] => 9999 [policy] => array ( [load] => 1 ) ) [1] => array ( [principal] => 1 [authority] => 9999 [policy] => array ( [add_children] => 1 [create] => 1 [copy] => 1 [delete] => 1 [list] => 1 [load] => 1 [move] => 1 [publish] => 1 [remove] => 1 [save] => 1 [steal_lock] => 1 [undelete] => 1 [unpublish] => 1 [view] => 1 ) ) [2] => array ( [principal] => 22 [authority] => 9999 [policy] => array ( [add_children] => 1 [create] => 1 [copy] => 1 [delete] => 1 [list] => 1 [load] => 1 [move] => 1 [publish] => 1 [remove] => 1 [save] => 1 [steal_lock] => 1 [undelete] => 1 [unpublish] => 1 [view] => 1 ) ) ) ) )
i using series of foreach(){foreach(){foreach(){}}} seemed messy , inefficient. having trouble getting head around this. ideas?
a recursive function should trick:
$values = array(17, 6, 3, 2, 5); function find($array, &$values) { foreach ($array $key => $element) { if (is_array($element)) { find($element, $values); } elseif ($key == 'principal') { foreach ($values $value) { if ($element == $value) { echo 'found' . php_eol; // stuff } } } } } find($array, $values);
Comments
Post a Comment