2 Replies - 999 Views - Last Post: 25 July 2011 - 08:57 PM Rate Topic: -----

#1 dtvrij74   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-July 11

PHP: Iterating through multidimensional array

Posted 25 July 2011 - 08:43 PM

Hello all! I am using a multidimensional array like this one:
$mycart=array(
	1=>array(1=>0,2=>0),
	2=>array(1=>0,2=>0)

I have attempted to construct a foreach loop to do the job of iterating through the array:
foreach($_SESSION['crt'] as $story){
    foreach($story as $quantity)
	{
		echo $story . $quantity . "<br />";
	}
}

(the above code works)
All the examples I have seen are ones like this:
...
$mycart=array(
1=>array(1=>0)
...

and not like this:
...
$mycart=array(
1=>array(1=>0, 2=>0)
...

So my question is, how do I access the "2" key?
I hope I am being clear enough.
Thanks!!!

--dtvrij74

Is This A Good Question/Topic? 0
  • +

Replies To: PHP: Iterating through multidimensional array

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: PHP: Iterating through multidimensional array

Posted 25 July 2011 - 08:52 PM

echo $mycart[1][2];

Was This Post Helpful? 1
  • +
  • -

#3 dtvrij74   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-July 11

Re: PHP: Iterating through multidimensional array

Posted 25 July 2011 - 08:57 PM

Thanks CTPHPMWB. It worked :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1