Jump to content


Photo

Surely a simple problem...


  • Please log in to reply
5 replies to this topic

#1 ViperSBT

ViperSBT
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 29 August 2006 - 03:24 PM

OK, I pass an array from another page, I then use the following code to handle the array:
foreach($states as $state){
		echo $state;}
I would expect to get each of the states in the array returned, instead what I get is:

ArrayArrayArrayArray

So, why isn't the array being parsed?  If I do print_r($states) I can see each of the elements in the array and they hold the correct value...  So it seems the foreach isn't doing what it is supposed.

#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 29 August 2006 - 03:34 PM

$states is an array of arrays.

#3 ViperSBT

ViperSBT
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 29 August 2006 - 03:39 PM

$states = Array ( [0] => Array ( [0] => 5 [1] => 30 ) )

As for the series of ArrayArray...  The foreach is inside a while loop...

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 29 August 2006 - 03:41 PM

OK, I pass an array from another page, I then use the following code to handle the array:

foreach($states as $state){
		echo $state;}
I would expect to get each of the states in the array returned, instead what I get is:

ArrayArrayArrayArray

So, why isn't the array being parsed?  If I do print_r($states) I can see each of the elements in the array and they hold the correct value...  So it seems the foreach isn't doing what it is supposed.

as jenk said, your values are being held within another level of arrays, so you need to access them that way. looks like you could probably simply do the following:
<?php
foreach ($states as $state) echo "$state[1]<br />\n";
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 29 August 2006 - 03:43 PM

If that is how you are building the $states array, you can't use a simple foreach loop to echo the results.  2D arrays have to be echoed how they are built, using both dimensions.

Info: PHP Manual


#6 ViperSBT

ViperSBT
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 29 August 2006 - 03:54 PM

The $states array is bein gbuilt from a Mutli-Seelction List field on a prior page.  The select which states they want to see a report for.
<select multiple name='states[]' size='10'>
			<option value='All' SELECTED>All States/Provinces</option>
			<?
			foreach ($statelist as $key => $value) {
			echo "<option value='" .$key."'>$statelist[$key]</option>\n";
			}
			?></select>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users