How to search by key=>value in a multidimensional array in PHP

前端 未结 15 2087
闹比i
闹比i 2020-11-22 00:13

Is there any fast way to get all subarrays where a key value pair was found in a multidimensional array? I can\'t say how deep the array will be.

Simple example arra

15条回答
  •  無奈伤痛
    2020-11-22 00:49

    I needed something similar, but to search for multidimensional array by value... I took John example and wrote

    function _search_array_by_value($array, $value) {
            $results = array();
            if (is_array($array)) {
                $found = array_search($value,$array);
                if ($found) {
                    $results[] = $found;
                }
                foreach ($array as $subarray)
                    $results = array_merge($results, $this->_search_array_by_value($subarray, $value));
            }
            return $results;
        }
    

    I hope it helps somebody :)

提交回复
热议问题