PHP:如何修改动态多维数组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP:如何修改动态多维数组相关的知识,希望对你有一定的参考价值。

我有一个用户可以动态创建树视图层次结构的功能。所以每次用户创建一个新的树视图我得到一组不同的数组,其中一个例子是这样的。所以我用户完成树视图创建后点击提交我只是直接将对象属性从js传递给php我无法更改js中的数据,因为它的插件vue-jstree所以我计划在php中更改它。但是,它没有修复数组,我可以告诉它是否只有2 forloop。删除或编辑所有这些键的逻辑甚至是多么深

Array
(
    [0] => Array
        (
            [id] => 0
            [text] => Hi-flyer
            [value] => Hi-flyer
            [icon] => fa fa-user
            [opened] => 1
            [selected] => 1
            [disabled] => 
            [loading] => 
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [text] => Cynthia Abanes
                            [value] => 5710
                            [icon] => fa fa-user
                            [opened] => 1
                            [selected] => 
                            [disabled] => 
                            [loading] => 
                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 2
                            [text] => Ronnie De Guzman
                            [value] => 5806
                            [icon] => fa fa-user
                            [opened] => 1
                            [selected] => 
                            [disabled] => 
                            [loading] => 
                            [children] => Array
                                (
                                )

                        )

                    [2] => Array
                        (
                            [id] => 3
                            [text] => Ronnie De Guzman
                            [value] => 5806
                            [icon] => fa fa-user
                            [opened] => 1
                            [selected] => 
                            [disabled] => 
                            [loading] => 
                            [children] => Array
                                (
                                )

                        )

                )

        )

)
答案

您可以使用RECURSIVE function执行此操作,如下所示,

$array = array("id" => "value1", "value" => "Test", "text" => "Test", "loading" => "100", "children" => array("id" => "value1","value" => "Test","text" => "Test","loading" => "100","children" => array("id" => "value1", "value" => "Test",)));

$keys = ["text", "loading"]; //add all keys which are you want to remove
//we loop all keys and run arrayRemove function over each key here
foreach($keys as $key){
  arrayRemove($array, $key);
}

function arrayRemove(&$array, $key) {
   unset($array[$key]);
   foreach ($array as &$value) {
      if (is_array($value)) {
         arrayRemove($value, $key);
      }
   }
}

我的阵列将与你的阵列不同,无论如何这将完成你的工作:)结果将如下,

Array ( [id] => value1 [value] => Test [children] => Array ( [id] => value1 [value] => Test [children] => Array ( [id] => value1 [value] => Test ) ) )
另一答案

你可以用它

<?php 
    for ($i=0; $i < count($yourArray); $i++) { 
        unset($yourArray[$i]['text']);
        unset($yourArray[$i]['icon']);
        unset($yourArray[$i]['opened']);
    }

    // then display to check
    var_dump($yourArray);

?>
另一答案

我看到你的代码并尝试解决你的问题。这是一个非常简单的递归过程,可以帮助您解决问题。只要看看我的代码,并尝试找到你想要的方式。

<?php
$data = array(
        array('id'=>1,'text'=>'hi-flyer','icon'=>'fa fa-user','opened'=>1,'selected'=>1,'disabled','loading','children'=>array()),
        array('id'=>2,'text'=>'hi-flyer','icon'=>'fa fa-user','opened'=>1,'selected'=>1,'disabled','loading','children'=>array(array('id'=>4,'text'=>'hi-flyer','icon'=>'fa fa-user','opened'=>1,'selected'=>1,'disabled','loading','children'=>array()),array('id'=>5,'text'=>'hi-flyer','icon'=>'fa fa-user','opened'=>1,'selected'=>1,'disabled','loading','children'=>array()))),
        array('id'=>3,'text'=>'hi-flyer','icon'=>'fa fa-user','opened'=>1,'selected'=>1,'disabled','loading','children'=>array())
);
$data = change_text_using_id(1,$data,"Hello world!");
print_all_id_text($data);


function change_text_using_id($id,$data,$text){
    for($i=0;$i<count($data);$i++){
        if($id == $data[$i]['id']){
            $data[$i]['text'] = $text;
            echo "success<br>";
        }else if(count($data[$i]['children'])>0){
            $data[$i]['children'] = change_text_using_id($id,$data[$i]['children'],$text);
        }
    }

    return $data;
}
function print_all_id_text($data){
    for($i=0;$i<count($data);$i++){
        echo "ID==> ".$data[$i]['id']." And Text==> ".$data[$i]['text'].'<br>';
        if(count($data[$i]['children'])>0){
            print_all_id_text($data[$i]['children']);
        }
    }
}

?>

谢谢。我想你找到了答案。编码快乐:D

以上是关于PHP:如何修改动态多维数组的主要内容,如果未能解决你的问题,请参考以下文章

PHP如何将嵌套的foreach变成数组的数组(多维数组)

PHP如何动态为多维数组的子数组分别添加元素

如何使用复选框形式选择 PHP 多维数组项

如何从数组值生成多维数组

求JS如何读取PHP里的多维数组?

如何比较两个PHP多维数组