derrida
asked on
help with looping an array
hi
i have this array and i need to loop it and sote several data in the database.
i attach the returning array.
each key is the position and if there are children i need an inner loop and store the keys as position the id and the parent id. my problem is with the inner loop and the parent id.
help will be appriciated.
best regards
i have this array and i need to loop it and sote several data in the database.
i attach the returning array.
each key is the position and if there are children i need an inner loop and store the keys as position the id and the parent id. my problem is with the inner loop and the parent id.
help will be appriciated.
best regards
this is the basic structure of the array:
array(4) {
[0]=>
array(1) {
["id"]=>
string(1) "1"
}
[1]=>
array(2) {
["id"]=>
string(2) "70"
["children"]=>
array(1) {
[0]=>
array(1) {
["id"]=>
string(2) "78"
}
}
}
[2]=>
array(1) {
["id"]=>
string(1) "4"
}
[3]=>
array(1) {
["id"]=>
string(1) "2"
}
}
Try this:
And this is the output
<?php
$myarr = array(
array("id"=>"1"),
array("id"=>"70", "children"=>array(array("id"=>"78"))),
array("id"=>"4"),
array("id"=>"2")
);
loop_me($myarr);
function loop_me($arr) {
for($i = 0, $n = count($arr); $i < $n; $i++) {
echo "Hi! my ID = " . $arr[$i]['id']."\n";
if (isset($arr[$i]['children'])) {
echo " I have ".count($arr[$i]['children'])." children.\n";
loop_me($arr[$i]['children']);
}
}
}
?>
And this is the output
Hi! my ID = 1
Hi! my ID = 70
I have 1 children.
Hi! my ID = 78
Hi! my ID = 4
Hi! my ID = 2
ASKER
hi
first thanks for helping.
this is in the right direction but i still have problem extracting the 3 parameters from this multidimentional array.
i need the position of every item: those without any children will be set to zero but the children should be the keys in the children (that is: [0][1][2] and so on).
the id of each item (top level or children.
and for each child the id of the parent.
thanks again
first thanks for helping.
this is in the right direction but i still have problem extracting the 3 parameters from this multidimentional array.
i need the position of every item: those without any children will be set to zero but the children should be the keys in the children (that is: [0][1][2] and so on).
the id of each item (top level or children.
and for each child the id of the parent.
thanks again
modifying the code from stingRay this may be all the info you want.
If this isn't the output you need then please give explicitly what you want as an output from the array input you have given in the question.
below is the code and its output
If this isn't the output you need then please give explicitly what you want as an output from the array input you have given in the question.
below is the code and its output
<?php
$myarr = array(
array("id"=>"1"),
array("id"=>"70", "children"=>array(array("id"=>"78"))),
array("id"=>"4"),
array("id"=>"2")
);
loop_me($myarr);
function loop_me($arr, $ancestor_ids='', $parent_index='', $tab='') {
$ancestor_ids .= ($ancestor_ids ? "." : "");
for($i = 0; $i < count($arr); $i++) {
echo $tab."Item[$i] ID=" . $arr[$i]['id'] , "\n";
echo $tab," my address by index is $parent_index","[$i]\n";
echo $tab," my address by id is $ancestor_ids",$arr[$i]['id'];
if (isset($arr[$i]['children'])) {
echo " I have ",count($arr[$i]['children'])," children\n";
loop_me($arr[$i]['children'], $ancestor_ids.$arr[$i]['id'], $parent_index."[".$i."]", $tab ."\t");
} else {
echo ".\n";
}
}
}
?>
Item[0] ID=1
my address by index is [0]
my address by id is 1.
Item[1] ID=70
my address by index is [1]
my address by id is 70 I have 1 children
Item[0] ID=78
my address by index is [1][0]
my address by id is 70.78.
Item[2] ID=4
my address by index is [2]
my address by id is 4.
Item[3] ID=2
my address by index is [3]
my address by id is 2.
ASKER
hi thanks for helping
the output i want so i could store each in the database is attached.
the output you gave seem ok but i need it more like the one ai attach.
it is so frastrating getting into issues everytime i need recursion.
the output i want so i could store each in the database is attached.
the output you gave seem ok but i need it more like the one ai attach.
it is so frastrating getting into issues everytime i need recursion.
Item[0] my ID=1
my position is = 0
my parent is = 0
Item[1] my ID=70
my position is = 0
my parent is 0 . I have 1 children
Item[0] my ID=78
my position inside my parent is = 0
my parent is = 70
Item[2] my ID=4
my position is = 0
my parent is = 0
Item[3] ID=2
my position is = 0
my parent is = 0
ASKER
as you can see in my output all top level items has 0 position and 0 parent.
abt the children should have their right position and parent.
abt the children should have their right position and parent.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
you are a GOD
by the way do you have a good material on recursives? i always get into trouble with them.
by the way do you have a good material on recursives? i always get into trouble with them.
ASKER