php - Return values of array if in_array() returns true -
i have 1 multi dimensinal array,
array ( [0] => array ( [item_no] => 1.01 [sor_id] => 2 [selected_qty] => 7 [price] => 3 [type] => s [form_name] => gsor [parent_id] => 89 ) [1] => array ( [item_no] => 1.03.03 [sor_id] => 7 [selected_qty] => 1 [price] => 50 [type] => s [form_name] => gsor [parent_id] => 89 ) [2] => array ( [item_no] => 1.23 [sor_id] => 28 [selected_qty] => 6 [price] => 60 [type] => s [form_name] => gsor [parent_id] => 89 ) [3] => array ( [item_no] => 6.03 [sor_id] => 64 [selected_qty] => 1 [price] => 50 [type] => s [form_name] => gsor [parent_id] => 61 ) [4] => array ( [item_no] => 4.02 [sor_id] => 42 [selected_qty] => 1 [price] => 39 [type] => s [form_name] => gsor [parent_id] => 40 ) )
i have 1 recursive function returns true if value exist in multi dimensional array other,
recursive in_array() function,
function in_array_r($needle, $haystack, $strict = false) { foreach ($haystack $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return true; // return $haystack; // print_r($haystack); } } return false; }
example,
echo $item_2 = in_array_r("1.03.03", $selected_items_array) ? 'found' : 'not found';
so, item_no
=> 1.03.03
exist in multi dimensional array returns true otherwise returns false, want values of price
,sor_id
of 1st position. returns 1
or '0' how return whole array or array index
using array or index can fetch values. or other options.
you there. instead of true, return id of matching element:
function in_array_r($needle, $haystack, $strict = false) { foreach ($haystack $key => $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return $key; } } return false; } $matching_item = in_array_r("1.03.03", $selected_items_array); echo $matching_item===false ? "not found" : " found @ item ".$matching_item;
Comments
Post a Comment