John Davidson

PHP - Chain array items through their children

0 comments
Message:


I have the following array of items in PHP :


0 => [
'name' => 'retail',
'id' => 1,
'following' => null
],
1 => [
'name' => 'industry',
'id' => 2,
'following' => 3
],
2 => [
'name' => 'entertainment',
'id' => 3,
'following' => 1
],
3 => [
'name' => 'sports',
'id' => 4,
'following' => null
],
4 => [
'name' => 'construction',
'id' => 5,
'following' => null
],
5 => [
'name' => 'music',
'id' => 6,
'following' => 5
]
...

and I would like to chain the names of each items and ordering them from parent to child. The example above would give the result :


[
0 => 'industry / entertainment / retail',
1 => 'sports',
2 => 'music / construction'
]

Each item could have as many as child as possible, and the order or the items is random. I am working on this since a few days and I have still no success. Any help please ?



My Answer:
array(
'id' => 1,
'name' => 'Item 1',
'parent_id' => null
);

1 = array(
'id' => 2,
'name' => 'Item 2',
'parent_id' => 1
);

2 = array(
'id' => 3,
'name' => 'Item 3',
'parent_id' => 2
);

3 = array(
'id' => 4,
'name' => 'Item 4',
'parent_id' => 3
);

4 = array(
'id' => 5,
'name' => 'Item 5',
'parent_id' => 4
);

5 = array(
'id' => 6,
'name' => 'Item 6',
'parent_id' => 5
);

6 = array(
'id' => 7,
'name' => 'Item 7',
'parent_id' => 6
);

7 = array(
'id' => 8,
'name' => 'Item 8',
'parent_id' => 7
);

8 = array(
'id' => 9,
'name' => 'Item 9',
'parent_id' => 8
);

9 = array(
'id' => 10,
'name' => 'Item 10',
'parent_id' => 9
);

);

I want to chain these items through their parent_id so that I can get the full chain of items starting from the root item. How can I achieve this in PHP?

php
function getChain($items, $parentId) {
$chain = [];
$currentItem = $items[$parentId];

while ($currentItem) {
$chain[] = $currentItem;
$parentId = $currentItem['parent_id'];
$currentItem = $parentId ? $items[$parentId] : null;
}

return array_reverse($chain);
}

$items = [
0 => ['id' => 1, 'name' => 'Item 1', 'parent_id' => null],
1 => ['id' => 2, 'name' => 'Item 2', 'parent_id' => 1],
2 => ['id' => 3, 'name' => 'Item 3', 'parent_id' => 2],
3 => ['id' => 4, 'name' => 'Item 4', 'parent_id' => 3],
4 => ['id' => 5, 'name' => 'Item 5', 'parent_id' => 4],
5 => ['id' => 6, 'name' => 'Item 6', 'parent_id' => 5],
6 => ['id' => 7, 'name' => 'Item 7', 'parent_id' => 6],
7 => ['id' => 8, 'name' => 'Item 8', 'parent_id' => 7],
8 => ['id' => 9, 'name' => 'Item 9', 'parent_id' => 8],
9 => ['id' => 10, 'name' => 'Item 10', 'parent_id' => 9]
];

$rootItemId = 0;
$chain = getChain($items, $rootItemId);

foreach ($chain as $item) {
echo $item['name'] . "\n";
}


This code defines a `getChain` function that takes the array of items and a parent ID as input and returns the chain of items starting from the root item. It then uses this function to get the chain of items starting from the root item (with ID 0) and prints out the names of the items in the chain.

Rate this post

3 of 5 based on 5794 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us