Reputation: 1200
Following code:
<?php
$test_array = array();
$test_array['string_index'] = "data in string index";
$test_array[] = "data in index 0";
$test_array[] = "data in index 1";
$test_array[] = "data in index 2";
foreach($test_array as $key => $val)
{
if($key != 'string_index')
{
echo $val."<br>";
}
}
?>
gives result:
data in index 1
data in index 2
Question is - where is "data in index 0"??? How to get elements from numeric indices 0-n? Also if I change 'string_index' to something else which doesn't exist, it echoes everything except [0]. Plz, explain me this.
Thnx in advance
Upvotes: 3
Views: 3291
Reputation: 1285
Use === or !== instead of ==, != respectively, you need to validate type cast with some strict comparison operators.
<?php
$test_array = array();
$test_array['string_index'] = "data in string index";
$test_array[] = "data in index 0";
$test_array[] = "data in index 1";
$test_array[] = "data in index 2";
foreach($test_array as $key => $val)
{
if($key ==='string_index')
{
//do something
}else{
echo $key.$val."<br>";
}
}
?>
Upvotes: 1
Reputation: 11286
To clarify:
The reason that 0 != 'string_index'
is that the string is apparently "downcast" to an integer in a string / integer comparison, and string_index
is parsed until a character is encountered that is not a digit, thereby evaluating to the empty string which equals 0.
Upvotes: 0
Reputation: 360622
That's because the 'index 0' data has key 0. In PHP, 0
(number zero), '0'
(string zero), and ''
(empty string) are all equivalent - they can get typecast to each other. If you'd simply done print_r($test_array)
, you'd get
Array
(
[string_index] => data in string index
[0] => data in index 0
[1] => data in index 1
[2] => data in index 2
)
The other option would have been to use the strict inequality test (!==
) which compares value AND type. In that case, 0 !== 'string index'
evaluates to true and everything works as expected.
Comment followup:
if you'd change the inside of your loop to this:
echo "key: $key (", gettype($key), ") val: $val (", gettype($val), ")\n";
if($key != 'string_index') {
echo "$key != 'string_index'\n";
} else {
echo "$key == 'string_index'\n";
}
You'll get:
key: string_index (string) val: data in string index (string)
string_index == 'string_index'
key: 0 (integer) val: data in index 0 (string)
0 == 'string_index'
key: 1 (integer) val: data in index 1 (string)
1 != 'string_index'
key: 2 (integer) val: data in index 2 (string)
2 != 'string_index'
As you can see, they're all there - it's your comparison that was failing you, because you didn't take PHP's typecasting/conversion rules into account.
Upvotes: 12
Reputation: 1637
Had me puzzled for a second too :) You just need to make it !== not !=
<?php
$test_array = array();
$test_array['string_index'] = "data in string index";
$test_array[0] = "data in index 0";
$test_array[] = "data in index 1";
$test_array[] = "data in index 2";
foreach($test_array as $key => $val)
{
if($key !== 'string_index')
{
echo $val."<br>";
}
}
?>
That fixes your issue.
Upvotes: 3