我具有一个函数,用户可以动态创建树视图层次结构。因此,每次用户创建一个新的treeview,我都会得到一组不同的数组,例如这样。因此,在用户完成TreeView之后,请单击"提交",我只是直接将对象属性从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
(
)
)
)
)
)
您可以使用递归函数像下面一样执行此操作,
$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