Reputation: 133
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.
Upvotes: 1
Views: 221
Reputation: 22162
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
).
Upvotes: 1
Reputation: 26789
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/>';
Upvotes: 1