Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中从嵌套数组中删除键和值_Php_Arrays_Multidimensional Array - Fatal编程技术网

在PHP中从嵌套数组中删除键和值

在PHP中从嵌套数组中删除键和值,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有以下PHP数组,其中一些元素在外部数组中重复一次,在内部数组中重复一次: array( 'content' => array( 'car' => '1', 'house' => '2', 'name' => 'a_name', ),

我有以下PHP数组,其中一些元素在外部数组中重复一次,在内部数组中重复一次:

        array(
            'content' =>
                array(
                    'car' => '1',
                    'house' => '2',
                    'name' => 'a_name',
                ),
            'restrictions' =>
                array(
                    'page_size' => 500,
                    'page_offset' => 0,
                ),
            'car' => '1',
            'house' => '2',
            'name' => 'a_partner_name',
            'criteria' => array(),
        );
我想知道如何执行以下操作(可能使用array\u walk\u recursive)

从数组中完全删除一个元素,例如,如果我删除“car”元素,则生成的数组将如下所示(请注意,数组中的2个“car”元素都已删除):

为数组中的元素设置一个值,例如,如果我将“car”元素的值从“1”更改为“2”,则生成的数组将如下所示(再次注意,它在两个位置发生了更改):

非常感谢

使用(&)使事情变得更简单(尽管我不推荐这样的结构)并删除

$innerArray = ['car' => 'some value'];

$outerArray = [
    'content' => &$innerArray,
    'car' => &$innerArray['car']
];

// change car to '2'
$outerArray['car'] = '2';
// is the same as
$innerArray['car'] = '2';
// is the same as
$outerArray['content']['car'] = '2';

// you can use this for deletion
function removeRecursive(&$array, $elem){
    foreach ($array as $key => &$value) {
        if($key === $elem)
            unset($array[$key]);
        elseif ($value instanceof Traversable || is_array($value))
            removeRecursive($value, $elem);
    }
}

removeRecursive($outerArray, 'car');

我只想颠倒在removeRecursive函数中检查的顺序。目前,它不会删除作为数组的元素。在if-else语句中使用大括号只是要求trouble@k0pernikus我们不要在这里讨论这个问题
         array(
            'content' =>
                array(
                    'car' => '2',
                    'house' => '2',
                    'name' => 'a_name',
                ),
            'restrictions' =>
                array(
                    'page_size' => 500,
                    'page_offset' => 0,
                ),
            'car' => '2',
            'house' => '2',
            'name' => 'a_partner_name',
            'criteria' => array(),
        );
$innerArray = ['car' => 'some value'];

$outerArray = [
    'content' => &$innerArray,
    'car' => &$innerArray['car']
];

// change car to '2'
$outerArray['car'] = '2';
// is the same as
$innerArray['car'] = '2';
// is the same as
$outerArray['content']['car'] = '2';

// you can use this for deletion
function removeRecursive(&$array, $elem){
    foreach ($array as $key => &$value) {
        if($key === $elem)
            unset($array[$key]);
        elseif ($value instanceof Traversable || is_array($value))
            removeRecursive($value, $elem);
    }
}

removeRecursive($outerArray, 'car');