TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-20-2013, 06:26 AM   #1 (permalink)
The Wanderer
 
Join Date: Feb 2013
Posts: 7
Thanks: 0
elizabethfreya is on a distinguished road
Default 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
elizabethfreya is offline  
Reply With Quote
Old 02-20-2013, 07:46 PM   #2 (permalink)
The Wanderer
 
OldManRiver's Avatar
 
Join Date: Feb 2013
Posts: 7
Thanks: 0
OldManRiver is on a distinguished road
Default

Quote:
Originally Posted by elizabethfreya View Post
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
OldManRiver is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Beginner Guide #1 Hershey Absolute Beginners 1 11-07-2011 10:42 PM
PHP Group Array "teams" and add up points godsdead General 3 07-15-2009 08:38 PM
PHP Compressor Kalle Script Giveaway 8 05-28-2008 12:14 AM
Problem with Prototype and PHP wiifanatic Javascript, AJAX, E4X 3 02-18-2008 01:39 AM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


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

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design