php - Check if value exist in array and get array from multidimensional array -
i have below php array:
array ( [0] => array ( [policy_id] => 1 [category_id] => 5 [limit_amount] => 11.00 [limit_amount2] => 23.00 ), [1] => array ( [policy_id] => 1 [category_id] => 7 [limit_amount] => 32.00 [limit_amount2] => 23.00 ), [2] => array ( [policy_id] => 1 [category_id] => 4 [limit_amount] => 12.00 [limit_amount2] => 12.00 ) ) now want 2 things:
- want check if
category_id = 7exists in array or not, , if there. - then complete array multidimensional array,
example, if category_id = 7 in array should output
array ([policy_id] => 1 , [category_id] => 7 , [limit_amount] => 32.00, [limit_amount2] => 23.00 ) i tried use in_array(), not required values,
thanks help,
you can use array_column before doing array_search (i assume data in $array):
$match = $array[array_search(7, array_column($array, "category_id"))]; if need first check whether entry exists, first check return value of array_search, , return distinctive when value not found, e.g. false:
$index = array_search(7, array_column($array, "category_id")); $match = $index === false ? false : $array[$index];
Comments
Post a Comment