TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Problem with array php (http://www.talkphp.com/advanced-php-programming/9484-problem-array-php.html)

elizabethfreya 02-20-2013 06:26 AM

Problem with array php
 
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

OldManRiver 02-20-2013 07:46 PM

Quote:

Originally Posted by elizabethfreya (Post 37451)
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


All times are GMT. The time now is 08:36 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0