PHP Question: how to array_intersect_assoc() recursively

后端 未结 2 1641
忘了有多久
忘了有多久 2021-01-05 04:56

Let\'s say I want to do this:

$a = array_intersect_assoc(
 array(
  \'key1\' => array(
   \'key2\' => \'value2\'
  ),
  \'key3\' => \'value3\',
  \'key4\' => \'va         


        
2条回答
  •  粉色の甜心
    2021-01-05 05:42

    function array_key_match_recursive(array $main, array $other, $i = 0, &$result = []) {
        foreach($main as $key => $value) {
            $k = sprintf('%s%s', str_repeat('=', $i), $key);
            if (!isset($other[$key])) {
                $result[$k][] = 'not key';
            }
            if (!is_array($value) && empty($other[$key])) {
                $result[$k][] = 'value empty';
            }
            if (is_array($value) && isset($other[$key])) {
                array_key_match_recursive($value, $other[$key], ++$i, $result);
            }
        }
    
        //return (bool) !$result;
        return $result;
    }
    

提交回复
热议问题