Get all children for a given parent id in array - php

I have an array like this:
array(
array(
'id' => 1,
'parent_id' => null
),
array(
'id' => 2,
'parent_id' => 1
),
array(
'id' => 3,
'parent_id' => 2
),
array(
'id' => 4,
'parent_id' => null
),
array(
'id' => 5,
'parent_id' => 4
)
);
How can i find all the childrens of a given parent_id including all grand children? For example the function will return 2,3 for parent_id 1.
Thanks.

// I corrected the array to fit your result 1 => 2,3
<?php
$test = array(
array(
'id' => 1,
'parent_id' => null
),
array(
'id' => 2,
'parent_id' => 1
),
array(
'id' => 3,
'parent_id' => 1
),
array(
'id' => 4,
'parent_id' => null
),
array(
'id' => 5,
'parent_id' => 4
)
);
// 1_2+3
$parent_childs = ARRAY();
foreach ($test AS $index => $child) {
if (!isset($child['parent_id'])) { continue; }
$parent_childs[$child['parent_id']][] = $child['id'];
}
echo '<pre>';var_dump($parent_childs); echo '</pre>';
?>

Related

php function for converting a recursively array to a list in right order

I have an external API that returns a category list:
Current array:
id | parent | name
3 | 6 | Sub
6 | 9 | Test
9 | 0 | Parent 1
2 | 0 | Parent 2
Need to be:
id | parent | name
9 | 0 | Parent 1
2 | 0 | Parent 2
6 | 9 | Test
3 | 6 | Sub
Now I will convert this list in the right order, (from parent to childs), so I can simple foreach this new array and insert it in my database (with my custom parent_id).
I have created a function, but this works not good:
// DOES NOT WORK!!
function sortArrayFromParentToChilds($array)
{
$output = [];
foreach ($array as $key => $a) {
// search in new array
if (empty($output)) {
$output[] = $a;
unset($array[$key]);
} else {
if (findSubCategory($a, $output)) {
array_push($output, $a);
} else {
array_unshift($output, $a);
}
unset($array[$key]);
}
}
return $output;
}
function findSubCategory($id, $output)
{
foreach ($output as $o) {
if ($o['parent'] == $id) {
return true;
}
}
return false;
}
$result = sortArrayFromParentToChilds($initial);
Who can help me to get this function working?
Updated: added some sample data:
$initial = array(
array(
'name' => 'Paul',
'id' => 4,
'parent' => 2
),
array(
'name' => 'Liz',
'id' => 5,
'parent' => 2
),
array(
'name' => 'Comus',
'id' => 6,
'parent' => 3
),
array(
'name' => 'Mai',
'id' => 7,
'parent' => 2
),
array(
'name' => 'Titus',
'id' => 8,
'parent' => 3
),
array(
'name' => 'Adult',
'id' => 9,
'parent' => 6
),
array(
'name' => 'People',
'id' => 2,
'parent' => 0
),
array(
'name' => 'Puppy',
'id' => 10,
'parent' => 8
),
array(
'name' => 'Programmers',
'id' => 11,
'parent' => 4
),
);
This solution looks for all elements that have no parents, stores them in the $sortArray and deletes them in $data.
$data = $initial;
$sortData = [];
while(count($data)){
$ids = array_column($data,'id');
foreach($data as $key => $row){
if(in_array($row['parent'],$ids)) continue;
$sortData[] = $row;
unset($data[$key]);
}
}
echo '<pre>';
var_export($sortData);
Output:
array (
0 =>
array (
'name' => 'Comus',
'id' => 6,
'parent' => 3,
),
1 =>
array (
'name' => 'Titus',
'id' => 8,
'parent' => 3,
),
2 =>
array (
'name' => 'People',
'id' => 2,
'parent' => 0,
),
3 =>
array (
'name' => 'Paul',
'id' => 4,
'parent' => 2,
),
4 =>
array (
'name' => 'Liz',
'id' => 5,
'parent' => 2,
),
5 =>
array (
'name' => 'Mai',
'id' => 7,
'parent' => 2,
),
6 =>
array (
'name' => 'Adult',
'id' => 9,
'parent' => 6,
),
7 =>
array (
'name' => 'Puppy',
'id' => 10,
'parent' => 8,
),
8 =>
array (
'name' => 'Programmers',
'id' => 11,
'parent' => 4,
),
)
Note: There must be no elements with id == parent in $data.
This should work.
function orderArray($arr){
$ordArr = array();
$parentKeys = array(0); // Top Level elements have 0 in parent field
$newParentKeys = array();
$goon = true;
// search for parentkeys
while($goon) {
$goon = false;
foreach($arr as $row) {
if (in_array( $row['parent'], $parentKeys)){ // There are kids
$newParentKeys[] = $row['id']; // future parent key
$ordArray[] = $row; // add element in order
$goon = true; // Keep going another level down
}
}
$parentKeys = $newParentKeys; // Update parent keys for the next level
$newParentKeys = array(); // clear this array
}
return $ordArray;
}

How to set value in nested array PHP

I would like to set level to each array that has children key in nested array. More deep that array then more big the level value.
Here's what I'm expected:
<?php
$array = array(
array(
'id' => 1,
'children' => array(
array(
'id' => 6,
'children' => array(
array(
'id' => 7,
),
array(
'id' => 8,
)
),
// Expected
'level' => '2',
),
array(
'id' => 9,
)
),
// Expected
'level' => '1'
),
array(
'id' => 2,
'children' => array(
array(
'id' => 10,
),
array(
'id' => 13,
'children' => array(
array(
'id' => 14,
),
array(
'id' => 19,
'children' => array(
array(
'id' => 20,
),
array(
'id' => 21,
),
array(
'id' => 22,
),
array(
'id' => 23,
),
)
// Expected
'level' => '3'
)
),
// Expected
'level' => '2'
),
),
// Expected
'level' => '1'
)
);
So far I make a function to set the level that has children key in nested array. But, the result is not what I'm expected.
<?php
$array = array(
array(
'id' => 1,
'children' => array(
array(
'id' => 6,
'children' => array(
array(
'id' => 7,
),
array(
'id' => 8,
)
),
// Expected
// 'level' => '2',
),
array(
'id' => 9,
)
),
// Expected
// 'level' => '1'
),
array(
'id' => 2,
'children' => array(
array(
'id' => 10,
),
array(
'id' => 13,
'children' => array(
array(
'id' => 14,
),
array(
'id' => 19,
'children' => array(
array(
'id' => 20,
),
array(
'id' => 21,
),
array(
'id' => 22,
),
array(
'id' => 23,
),
)
// Reality result
// 'level' => '4'
)
),
// Reality result
// 'level' => '3'
),
),
// Reality result
// 'level' => '2'
)
);
As you can see in Reality result comment is level 2, level 3, and level 4. What I'm expected the result is level 1, level 2, level 3, level (n+1). When there's not any children in the deeper array then the level value must be reset to level 1.
Here's my code that get the reality result:
<?php
function updateTreeArray(&$array, $level = 1) {
foreach ($array as $key => &$value) {
if (isset($value['children'])) {
$value['level'] = $level;
$level = setLevel($value['children'], $level);
}
if (is_array($value)) {
updateTreeArray($value, $level);
}
}
return $array;
}
function setLevel($array, $level) {
foreach ($array as $key => &$value) {
if (isset($value['children'])) {
$level += 1;
setLevel($value['children'], $level);
}
}
return $level;
}
print_r(updateTreeArray($array));
Could you please help me what should I do to achieve the expected result? Thanks.
One recursive function is enough.
function updateTreeArray(&$array, $level = 1)
{
foreach($array as &$item) {
if (array_key_exists('children', $item) && is_array($item['children'])) {
$item['level'] = $level;
updateTreeArray($item['children'], $level + 1);
}
}
}
updateTreeArray($array);
print_r($array);
fiddle

Merge php array with dynamic key

If I query out the array like this form:
$arr1:
0 =>
'id' => 1,
'name' => 'a'
1 =>
'id' => 2,
'name' => 'b'
2 =>
'id' => 3,
'name' => 'c'
3 =>
'id' => 4,
'name' => 'd'
$arr2:
0 =>
'id' => 1,
'parent' => '1a'
1 =>
'id' => 2,
'parent' => '2b'
2 =>
'id' => 3,
'parent' => '3c'
3 =>
'id' => 4,
'parent' => '4d'
When I need to merge these two I can done it using foreach loop.
The problem comes to when the $arr1 is dynamic data(need to use for pagination), i can't merge well using array_merge due to $arr2 is fixed data.
example:
first time:
$arr1:
0 =>
'id' => 1,
'name' => 'a'
1 =>
'id' => 2,
'name' => 'b'
$arr2:
0 =>
'id' => 1,
'parent' => '1a'
1 =>
'id' => 2,
'parent' => '2b'
2 =>
'id' => 3,
'parent' => '3c'
3 =>
'id' => 4,
'parent' => '4d'
second time:
$arr1:
0 =>
'id' => 3,
'name' => 'c'
1 =>
'id' => 4,
'name' => 'd'
$arr2:
0 =>
'id' => 1,
'parent' => '1a'
1 =>
'id' => 2,
'parent' => '2b'
2 =>
'id' => 3,
'parent' => '3c'
3 =>
'id' => 4,
'parent' => '4d'
I had try using foreach loop
foreach($arr1 as $k => $value){
$group[$k]['parent'] = $arr2[$k]['parent'];
$group[$k]['name'] = $value['name'];
$group[$k]['id'] = $value['id'];
}
It comes to the parent value does not change as $key is fixed.
The expected output should all the element in 1 array:
0 =>
'id' => 1,
'parent' => '1a'
'name'='a'
1 =>
'id' => 2,
'parent' => '2b'
'name'='b'
I think this is what you need...
The easiest way to do this is to re-index the second array with the ID as the key. I use array_column() to do this. Then use the id from $arr1 to access the parent value from the newly indexed array...
$parent = array_column($arr2, 'parent', 'id');
$group = [];
foreach($arr1 as $k => $value){
$group[$k]['parent'] = $parent[$value['id']];
$group[$k]['name'] = $value['name'];
$group[$k]['id'] = $value['id'];
}
In case $arr2 has more information, just use null as the second parameter to array_column(). Then you need to add the column name when you access that array...
$parent = array_column($arr2, null, 'id');
$group = [];
foreach($arr1 as $k => $value){
$group[$k]['parent'] = $parent[$value['id']]['parent'];
$group[$k]['name'] = $value['name'];
$group[$k]['id'] = $value['id'];
}
Try this:
foreach($arr1 as $k=>$v) {
$arr1[$k]['parent'] = $arr2[$k]['parent'];
}
print_r( $arr1 );

Add values from multidimensional array to another array

Hello good morning wherever you are :D, i have a lil problem, i have this code of arrays
$arrayToView is the info of every single user that i want.
$tagsArray are only tags that use every user but i need to merge all the info something like the last array...
$arrayToView = array(
'IVOFACUNDO' = array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23
),
'ESRAYCU' = array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44
)
)
And i have another one like this
$tagsArray= array(
'IVOFACUNDO' = array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
),
'ESRAYCU' = array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
)
so the question is how i can merge both arrays obviously respectively with the same admin something like this
$arrayToView = array(
'IVOFACUNDO' = array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23,
'tags' => array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
),
'ESRAYCU' = array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44,
'tags' => array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
)
)
The key 'tags' need to be created in the merge of every iteration to add and get one array with all the values, how i can do this?
You can try this snippet.
foreach($arrayToView as $key => $arr){
if(array_key_exists($key, $tagsArray)){
$arrayToView[$key]['tags'] = $tagsArray[$key];
}
}
echo '<pre>';print_r($arrayToView);echo '</pre>';
Use php inbuilt function
$result_Arr = array_merge_recursive($arrayToView,$tagsArray);
<?php
$arrayToView = array(
'IVOFACUNDO' => array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23
),
'ESRAYCU' => array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44
)
);
$tagsArray= array(
'IVOFACUNDO' => array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
),
'ESRAYCU' => array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
);
foreach($arrayToView as $key => $value){
if(isset($tagsArray[$key])){
$arrayToView[$key]['tags'] = array();
foreach($tagsArray[$key] as $key2 => $value2){
$arrayToView[$key]['tags'][$key2] = $tagsArray[$key][$key2];
}
}
}
echo'<pre>';
print_r($arrayToView);
echo'</pre>';
?>

Check if key exist in array if so, count it and create a new array php

From my SQL query, I will get the following array as a result. This is the last result I can get from SQL.I cant change any SQL because of some constraint.
I need to check if the same id exists or not and if it does need to count them and remove one of the duplicate array having the same id.
Sample array is
$array = array(
0 => array(
'id' => '17',
'status' => 1,
),
1 => array(
'id' => '18',
'status' => 1,
),
2 => array(
'id' => '21',
'status' => 1,
),
3 => array(
'id' => '5',
'status' => 2,
),
4 => array(
'id' => '18',
'status' => 1,
),
5 => array(
'id' => '22',
'status' => 5,
),
6 => array(
'id' => '6',
'status' => 1,
),
);
I need to check if they have a duplicate id or not, if yes need to count them and remove one of the duplicates.We need to preserve the array structure.
End Results should be
array(
0 => array(
'id' => '17',
'status' => 1,
'count'=1,
),
1 => array(
'id' => '18',
'status' => 1,
'count'=2,
),
2 => array(
'id' => '21',
'status' => 1,
'count'=1,
),
3 => array(
'id' => '5',
'status' => 2,
'count'=1,
),
4 => array(
'id' => '22',
'status' => 5,
'count'=1,
),
5 => array(
'id' => '6',
'status' => 1,
'count'==>1,
),
)
You can Check it by this way but duplicate entry still in the array.
$ids = array();
foreach ($array as $key => $value)
{
$ids[] = $value['id'];
$count = array_count_values($ids);
}
for($i = 0; $i < count($array);$i++)
{
$array[$i]['count'] = $count[$array[$i]['id']];
}
You can do this with the array_unique function.
Use it like so:
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
Which outputs:
Array ( [a] => red [b] => green )

Categories