- Moderated by:
- Support Team
-
- rank:
-
Professional
- registered:
- December 2002
- Status:
- offline
- last visit:
- 24.08.08
- Posts:
- 1588
Hello old friends! I am totally stumped on this... :(
Required: traversal of muliti-dimensional array where dimensions are variable - this is a node type affair. Example input array as follows:
Code
Remember the above is completely variable in regards to dimension!
Required output (based on the above):
Code
array
(
[0] => array
(
[id] => 310
[path] => >310>
[positon] => 1
)
[1] => array
(
[id] => 297
[path] => >310>297>
[positon] => 1
)
[2] => array
(
[id] => 291
[path] => >310>291>
[positon] => 2
)
[3] => array
(
[id] => 315
[path] => >310>291>315>
[positon] => 1
)
[4] => array
(
[id] => 316
[path] => >310>291>316>
[positon] => 2
)
[5] => array
(
[id] => 308
[path] => >310>308>
[positon] => 3
)
[6] => array
(
[id] => 317
[path] => >310>308>317>
[positon] => 1
)
[7] => array
(
[id] => 318
[path] => >310>308>318>
[positon] => 2
)
[8] => array
(
[id] => 319
[path] => >310>308>319>
[positon] => 3
)
[9] => array
(
[id] => 309
[path] => >310>300>
[positon] => 4
)
[10] => array
(
[id] => 298
[path] => >310>298>
[positon] => 5
)
[11] => array
(
[id] => 296
[path] => >310>296>
[positon] => 6
)
)
The first dimension order is not so important as I will just be looping thru and using the id, path and position.
id - this is the key of each dimension of the array, always unique
path - MUST be in this format - shows the path of parents AND id (look at the input array - see how each parent array key creates this.
position - this shows the position of the array in regards to other siblings in the same parent array - this will be always in the same order as that found in the input array.
Thanks for any advice you can give...
-Webvida
--
-Lobos
Professional PHP Framework Services: Concept, Development and Deployment -
- rank:
-
Professional
- registered:
- September 2006
- Status:
- online
- Posts:
- 1451
A recursive function that i don't know when stop...
in this case, can be an empty check, but sure you'll need to define a criteria depending of your leaf info (!isset($value[field]){check again})...
This function works in my head:
Code
function getinfo($matrix, $path) {
$position = 1;
$info = array();
foreach ($matrix as $key => $value) {
$info[] = array('id' => $key, 'path' => "$path$key>", 'position' => $position);
if(!empty($value)) {
$internalinfo = getinfo($value, "$path$key>");
array_push($info, $internalinfo);
}
$position++;
}
return $info;
}
$result = getinfo($yourmultidimensionalmatrix, '>');
--
- Mateo T. -
Mis principios... son mis fines -
- rank:
-
Professional
- registered:
- December 2002
- Status:
- offline
- last visit:
- 24.08.08
- Posts:
- 1588
Thanks mate, this looks like it will work. The only thing is the position, this is the position of siblings under a parent and not the position in regards to the array as a whole. But anyways you have provided me enough to start with and for this I am grateful :)
-Lobos
--
-Lobos
Professional PHP Framework Services: Concept, Development and Deployment
