02-20-2013, 07:46 PM
|
#2 (permalink)
|
|
The Wanderer
Join Date: Feb 2013
Posts: 7
Thanks: 0
|
Quote:
Originally Posted by elizabethfreya
Hey guys im having an issue trying to iterate into an array. This is the array:
stdClass Object ( [new_occ] => Array ( [0] => Array ( [adults] => 2 [children] => Array ( [0] => 0 [1] => 0 [2] => 0 ) ) [1] => Array ( [adults] => 1 ) [2] => Array ( [adults] => 2 [children] => Array ( [0] => 0 [1] => 0 [2] => 0 ) ) ) )
The principal array nodes are rooms, i want to get them so i can print something like this.
(e.g)
Rooms Adults Children
1 1 1
2 2 2
Thanks in advance.
___________________________________________
Wholesale Handbags
|
EF,
You need foreach, but you have nested arrays so will go like this:
Code:
$my_array = new_occ;
foreach ($my_array as $key => $val) {
foreach ($val as $kk => $vv) {
echo "K=> $kk V=> $vv <br>";
} // end foreach $val
} // end foreach $my_array
If you are running from commandline instead of browser sub "\n" in for the "<br>".
If $vv displays as "Array" then you need an additional nested foreach.
If all displays OK, then put the logic to work that you need, instead of the "echo" statement.
If you need to track original key use this:
Code:
$my_array = new_occ;
foreach ($my_array as $key => $val) {
foreach ($val as $kk => $vv) {
echo "KY=> $key K=> $kk V=> $vv <br>";
} // end foreach $val
} // end foreach $my_array
Cheers!
OMR
|
|
|
|