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
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"
  }
}

Open in new window

LVL 1
derridaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

derridaAuthor Commented:
it need to be some sort of recursive since children can have children. but i am so stuck with it
0
StingRaYCommented:
Try this:

<?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']);
		}
	}
}
?>

Open in new window


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

Open in new window

0
derridaAuthor Commented:
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
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

ropennerCommented:
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
<?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";
		}
	}
}
?>

Open in new window

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.

Open in new window

0
derridaAuthor Commented:
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.
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

Open in new window

0
derridaAuthor Commented:
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.
0
StingRaYCommented:
Then is this what you want?

<?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, $parrent=0) {
        for($i = 0; $i < count($arr); $i++) {
                echo "Item[$i] ID=" . $arr[$i]['id'] , "\n";
                echo " my position is ".$i."\n";
                echo " my parent id is ".$parrent."\n";          
                if (isset($arr[$i]['children'])) {
                        loop_me($arr[$i]['children'], $arr[$i]['id']);
                }
        }
}
?>

Open in new window


The output is:

Item[0] ID=1
 my position is 0
 my parent id is 0
Item[1] ID=70
 my position is 1
 my parent id is 0
Item[0] ID=78
 my position is 0
 my parent id is 70
Item[2] ID=4
 my position is 2
 my parent id is 0
Item[3] ID=2
 my position is 3
 my parent id is 0

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
derridaAuthor Commented:
you are a GOD

by the way do you have a good material on recursives? i always get into trouble with them.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.