-1

I have a function that user can create treeview hierarchy dynamically. So everytime user create a new treeview I got a different set of array one of example is like this. So I after user done creating of treeview then click submit I just directly pass the object property from js to php I can't change the data from js because its plugin vue-jstree so what I'm planning is to change it in php. However, its not fix set of array that I could tell whether its only a 2 forloop. What logic to remove or Edit all those keys even how deep it is

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
                                (
                                )

                        )

                )

        )

)
BGTabulation BGTabulate
  • 1,677
  • 3
  • 16
  • 39

3 Answers3

1

You can use RECURSIVE function to do this like below,

$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);
      }
   }
}

My array will be different from your one, anyway this will do your work :) and result will be like below,

Array ( [id] => value1 [value] => Test [children] => Array ( [id] => value1 [value] => Test [children] => Array ( [id] => value1 [value] => Test ) ) )
Casper
  • 1,469
  • 10
  • 19
0

You could use this

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

?>
Miggy
  • 816
  • 7
  • 16
0

I see your code and try to solve your problem. Here is very simple recursion process which can help to solve your problem. Just see my code and try to find your desire way.

<?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']);
        }
    }
}

?>

Thank you. I think you found your answer. Happy coding :D

Jakir Hossen
  • 451
  • 4
  • 13