php - Filter a multidemensional array -



php - Filter a multidemensional array -

i have array looks kinda this:

array(1) { ["special"]=> array(4) { [0]=> array(2) { ["id"]=> int(1) ["visitors"]=> int(2) } [1]=> array(2) { ["id"]=> int(4) ["visitors"]=> int(5) } [2]=> array(2) { ["id"]=> int(169) ["visitors"]=> int(0) } } }

how can filter 'id' value, result looking (if need arrays id = 4):

array(1) { ["special"]=> array(4) { [1]=> array(2) { ["id"]=> int(4) ["visitors"]=> int(5) } } }

i tried utilize function, doesn't homecoming need:

function search($array, $key, $value) { $results = array(); if (is_array($array)) { if (isset($array[$key]) && $array[$key] == $value) { $results[] = $array; } foreach ($array $subarray) { $results = array_merge($results, search($subarray, $key, $value)); } } homecoming $results; }

you can utilize array_filter this:

$result = array( "special" => array_filter($array["special"], function($element) { homecoming $element["id"] == 4; }) );

php arrays multidimensional-array filter

Comments

Popular posts from this blog

Using Android Volley to post an array to PHP -

python - How to add an model instance to a django queryset? -

angularjs - No 'Access-Control-Allow-Origin' error from local domain to public API -