Reputation: 2401
Following is my array output.
Array
(
[0] => Array
(
[id] => 1011
[user_id] => 168
[item_id] => 831
[post_content] => My New Post 20
[parent_comment_id] => 1010
[name] => a
[children] => Array
(
[0] => Array
(
[id] => 1012
[user_id] => 168
[item_id] => 831
[parent_comment_id] => 1011
[name] => a
[children] => Array
(
[0] => Array
(
[id] => 1013
[user_id] => 179
[item_id] => 831
[parent_comment_id] => 1012
[name] => a
[children] => Array
(
[0] => Array
(
[id] => 1014
[user_id] => 168
[item_id] => 831
[parent_comment_id] => 1013
[name] => a
)
)
)
)
)
)
)
)
I only want to remove following array keys only from children array.
item_id
parent_comment_id
name
I have tried to remove but it also remove from starting array also , I just want to remove that keys only from children array.
Following is my code...
$arr = array(
array('id'=>1011, 'user_id' => 168, 'item_id'=>831, 'post_content'=>'My New Post 20', 'parent_comment_id'=>1010, 'name'=>'a'),
array('id'=>1012,'user_id' => 168, 'item_id'=>831 ,'parent_comment_id'=>1011, 'name'=>'a'),
array('id'=>1013, 'user_id' => 179, 'item_id'=>831, 'parent_comment_id'=>1012, 'name'=>'a'),
array('id'=>1014,'user_id' => 168, 'item_id'=>831, 'parent_comment_id'=>1013, 'name'=>'a'),
);
echo "<pre> add";print_r($arr);
$new = array();
foreach ($arr as $a){
$new[$a['parent_comment_id']][] = $a;
}
$tree = createTree($new, array($arr[0]));
print_r($tree);
function createTree(&$list, $parent){
$tree = array();
foreach ($parent as $k=>$l){
if(isset($list[$l['id']])){
$l['children'] = createTree($list, $list[$l['id']]);
//unset($l['item_id']);
}
$tree[] = $l;
}
return $tree;
}
Upvotes: 0
Views: 36
Reputation: 1651
You can use flag for delete in children items, like this:
$arr = array(
array('id'=>1011, 'user_id' => 168, 'item_id'=>831, 'post_content'=>'My New Post 20', 'parent_comment_id'=>1010, 'name'=>'a'),
array('id'=>1012,'user_id' => 168, 'item_id'=>831 ,'parent_comment_id'=>1011, 'name'=>'a'),
array('id'=>1013, 'user_id' => 179, 'item_id'=>831, 'parent_comment_id'=>1012, 'name'=>'a'),
array('id'=>1014,'user_id' => 168, 'item_id'=>831, 'parent_comment_id'=>1013, 'name'=>'a'),
);
echo "<pre> add";print_r($arr);
$new = array();
foreach ($arr as $a){
$new[$a['parent_comment_id']][] = $a;
}
$tree = createTree($new, array($arr[0]));
print_r($tree);
function createTree(&$list, $parent, $isChild = false){
$tree = array();
foreach ($parent as $k=>$l){
if(isset($list[$l['id']])){
$l['children'] = createTree($list, $list[$l['id']], true);
if ($isChild) {
unset($l['item_id']);
// unset another field
}
}
$tree[] = $l;
}
return $tree;
}
Upvotes: 0
Reputation: 57121
This just involves a slight change to the recursive function. I've added a new parameter which flags if this is the base of the tree. Before the code adds the new node into the tree, if it is not the base it will remove the required elements first.
The subsequent calls all pass in false, to flag it is not the base call...
function createTree(&$list, $parent, $base = true){
$tree = array();
foreach ($parent as $k=>$l){
if(isset($list[$l['id']])){
$l['children'] = createTree($list, $list[$l['id']], false);
}
if ( ! $base ) {
unset($l['item_id']);
unset($l['parent_comment_id']);
unset($l['name']);
}
$tree[] = $l;
}
return $tree;
}
Upvotes: 1