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

问题描述 投票:-1回答:3

我有一个用户可以动态创建树视图层次结构的功能。所以每次用户创建一个新的树视图我得到一组不同的数组,其中一个例子是这样的。所以我用户完成树视图创建后点击提交我只是直接将对象属性从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
                                (
                                )

                        )

                )

        )

)
javascript php arrays treeview
3个回答
1
投票

您可以使用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 ) ) )

0
投票

你可以用它

<?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);

?>

0
投票

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

<?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

© www.soinside.com 2019 - 2024. All rights reserved.