1

I have a multidimensional array as follow:-

$worksheet = array(
  'sheet 1' => array(
    '#1 sheet 1',
    ' #2 sheet 1',
    '#3 sheet 1'
  ),

  'sheet 2' => array(
    '#1 sheet 2',
    '#2 sheet 2'
  ),

  'sheet 3' => array(
    '#1 sheet 3',
    '#2 sheet 3'
  )
);

then I run PHP code as below:

foreach($worksheet as $ws=>$value)
  echo $ws.'<br/>';
{
  foreach($value as $sheet=>$ivalue)
  {
    echo $ivalue.'<br/>';
  }
}

Above code will generate only the last array like follow:

sheet 1,  sheet 2,  sheet 3,  #1 sheet 3,  #2 sheet 3

what had happen to my:

#1 sheet 1, #2 sheet 1, #3 sheet 1, #1 sheet 2, #2 sheet 2.
2
  • Try to clean up your indentation before asking. Commented Dec 27, 2011 at 1:38
  • Updated indentation to make the code more readable. @FirstLast, please edit your question to move echo $ws.'<br/>'; where it actually lives in your code. Commented Dec 27, 2011 at 1:41

3 Answers 3

1

Fix your syntax. echo is not inside the {}s as you intend. Otherwise, PHP will assume that you just intend to do the echo, and won't realize you want to do an internal loop.

To fix:

foreach($worksheet as $ws=>$value)
{
     echo $ws.'<br/>';
Sign up to request clarification or add additional context in comments.

Comments

1

Because you did a mistake. The second and the third lines of your code are reversed. The code should be like this:

foreach($worksheet as $ws=>$value)
{
         echo $ws.'<br/>';

Thus, your code is looping with the external foreach through the echo and then, in a "section" enclosed by the brackets {, is doing another loop (the inner foreach).

Comments

1
echo $ws.'<br/>'

should be inside the brackets

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.