0

I am having trouble with a PHP foreach loop....I know how they work, I think I'm getting unexpected results. A bug perhaps? Am I missing something?

if(is_array($array)){

foreach ($array as $k => $v){

echo "<b>#$k</b><br />";

foreach ($v as $kk => $vv){

foreach ($vv as $kkk => $vvv){

if($kkk == "name"){echo "$kkk = NAME | ";}
if($kkk == "comment"){echo "$kkk = COMMENT | ";}

echo "<br />";

}

}

}

}else{
echo "Arguement is not a proper code array.";
}

The problem is that both of these lines are returning true and echoing the $kkk = xxx statement, but clearly you can see that only one of these lines should ever return true at a time.

if($kkk == "name"){echo "$kkk = NAME | ";}
if($kkk == "comment"){echo "$kkk = COMMENT | ";}

Here is the output I am getting, data is from a fiel upload:

#007 - Tomorrow Never Dies#slus-00975
name = NAME |
0 = NAME | 0 = COMMENT |
name = NAME |
0 = NAME | 0 = COMMENT |
name = NAME |
0 = NAME | 0 = COMMENT |
name = NAME |
0 = NAME | 0 = COMMENT |
#007 - Tomorrow Never Dies#slps-00000
name = NAME |
0 = NAME | 0 = COMMENT |
name = NAME |
0 = NAME | 0 = COMMENT |

I don't understand how this is happening, I would expect this if I was only using one '=' sign in the IF statement or something, but this has got me scratching my head.

Thanks!

6
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by pritaeas
0

echo your array structure. before everything and check is it as expected

<?php
echo "<pre>";
print_r($array);
echo "<pre>";

if(is_array($array)){
.
.
.
?>

Edited by urtrivedi: n/a

0

I believe the content is irrelivant to this particular problem which is why I didn't post it. I don't want to focus on the content because the foreach loops are looping properly but the if statements are not working properly. Everything is fine except the IF statements. Here is the content anyways:

Array
(
[007 - Tomorrow Never Dies#slus-00975] => Array
(
[1] => Array
(
[name] => Infinite Lives
[0] => 8001E096 0007
)
 
[2] => Array
(
[name] => Infinite Pk7 Ammo
[0] => 800EA846 000F
)
 
[3] => Array
(
[name] => Infinite Assault Rifle Ammo
[0] => 800EA8B2 000F
)
 
[4] => Array
(
[name] => Infinite Medikits
[0] => 800EA9DC 0007
)
 
)
 
[007 - Tomorrow Never Dies#slps-00000] => Array
(
[5] => Array
(
[name] => Infinite Lives
[0] => 8001E07A 0063
)
 
[6] => Array
(
[name] => Can Select All Mission
[0] => 8001E224 0101
[1] => 8001E226 0101
[2] => 8001E228 0101
[3] => 8001E22A 0101
[4] => 8001E22C 0101
)

)

)

Look at the original post, look closely at the if statements, now look at the results it gives below. You see how, for instance, line 3 says "0 = NAME | 0 = COMMENT |"? That is the result of the if statement in which $kkk is == both "name" and "content" according to the results.

So here is the code again that is not working as expected:

if($kkk == "name"){echo "$kkk = NAME | ";}
    if($kkk == "comment"){echo "$kkk = COMMENT | ";}

And those lines are producing one of these lines on each loop through:

name = NAME |
0 = NAME | 0 = COMMENT |
name = NAME |
0 = NAME | 0 = COMMENT |

It just doesn't make any sense. It should not do this, right?

Unless, somehow $kkk being 0 somehow changes things...I'm gonna try to run it again with $kkk in quotes maybe.

0

Ok, so I added quotes around the $kkk in the if statements and it actually worked!

Anyone know why it's like this?

I mean, this is basically what is happening, though I'd need to test it...

$kkk = 0;

if($kkk == "name"){} //  returns true
if("$kkk" == "name"){} // returns false

Ideas?

Edited by cjohnweb: n/a

0

[name] => Infinite Lives
[0] => 8001E096 0007

You put both numeric and associative in same matter. It is the problem.
Why your one array key is associative type and other numeric ? Try to build the array structure.

0
[007 - Tomorrow Never Dies#slps-00000] => Array
(
[5] => Array
(
[name] => Infinite Lives
[0] => 8001E07A 0063
)
 
[6] => Array
(
[name] => Can Select All Mission
[0] => 8001E224 0101
[1] => 8001E226 0101
[2] => 8001E228 0101
[3] => 8001E22A 0101
[4] => 8001E22C 0101
)

I have to admit, zero13 has a point, why not try for this (pseudoarray structure):

$group[0]
  ['code'] = "007 - Tomorrow Never Dies#slps-00000"
  ['name'][0]
     ['title'] = "Infinite Lives"
     ['items']
        [0] = "8001E07A 0063"
  ['name'][1]
     ['title'] = "Can Select All Mission"
     ['items']
        [0] = "8001E224 0101"
        [1] = "8001E226 0101"
        [2] = "8001E228 0101"
        [3] = "8001E22A 0101"
        [4] = "8001E22C 0101"
$group[1]
 ...
0

I understand that, depending on input, one, two or three entries are associative and the rest are numeric.

I don't understand why setting a variable to a numeric string make if statements go wacky.

The keys of the arrays are only used to identify what information each contains. The numeric ones are the codes, name is the code name, comment contains the code comments, etc. Straight forward.

But basically, like my example code above, why is a variable set to a number different than the same numeric string?

0

I guess I could do that. Is it "bad practice" to mix assoc & numeric arrays? Rather, is there a specific reason why to not do this (aside from the issue I ran into?)?

Thanks for all the input!

1

Explained in the manual.

Ok, so I added quotes around the $kkk in the if statements and it actually worked!

Anyone know why it's like this?

I mean, this is basically what is happening, though I'd need to test it...

$kkk = 0;

if($kkk == "name"){} //  returns true
if("$kkk" == "name"){} // returns false

Ideas?

Votes + Comments
encyclopaedic as ever :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.