0

I have an array as

$arrTest = array('val1','val2','val3','val4');
$arrTest['lastKey'] = 'Last Key';
foreach($arrTest as $key => $val) {
  if($key == 'lastKey') {
     echo "last found";
  }
}

The above code is not working. I have added associative element in the array. Could it be the reason?

6
  • "Not working" means what exactly - the last key is never found? Commented May 23, 2011 at 12:11
  • 1
    do you get any errors? I just ran your code and it echoed out... In fact it echoed twice... If you make the comparison operator check for identical you only get one echo as expected.... Though I'm not sure why. anyone care to elaborate? Commented May 23, 2011 at 12:11
  • I tried to run this code, and it writes "last found" twice. Really interesting. Commented May 23, 2011 at 12:15
  • 1
    down voted for not clear question Commented May 23, 2011 at 12:17
  • I am sorry for posting unclear Question. Actually it echos the "last found " twice for the first and last element i.e for 0 element also Commented May 23, 2011 at 12:26

7 Answers 7

6

Change == to === in:

if($key == 'lastKey')

Your existing code echos last found twice, once for key 0 and once for key lastKey.

Comparing integer 0 and string 'lastKey' using == returns true !!

From the PHP manual:

String conversion to numbers

When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

The string will be evaluated as a float if it contains any of the characters '.', 'e', or 'E'. Otherwise, it will be evaluated as an integer.

The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.

Sign up to request clarification or add additional context in comments.

2 Comments

Why comparison for 0 returns true?
@Umar: I've added the reason for this rather strange behavior.
4

Use === to compare. Because when key 0 will be compared with string lastKey, string will be converted to integer and false result will be returned.
http://codepad.org/5QYIeL4f

$arrTest = array('val1','val2','val3','val4');
$arrTest['lastKey'] = 'Last Key';
foreach($arrTest as $key => $val) {
  if($key === 'lastKey') {
     echo "last found";
  }
}

Read more about differences: http://php.net/manual/en/language.operators.comparison.php

2 Comments

no doubt false will be returned for the comparison 0 but its displaying last found twice which means its returns true in case of 0 comparison
I mean false result, not just false as value :) Sorry for my broken English.
3

You need to change your equality condition to check the type as well.

if($key === 'lastKey')

This is because PHP evaluates ' ' == 0 as true.

Comments

2

When I ran your code, 'last found' was outputted twice. 'lastKey' is evaluated to 0 in PHP, so if($key == 'lastKey') actually matches twices: once for 0 and once for your special element.

Comments

0

use the end() function to get the last key of an array and compare it in your if statement.

$arrTest = array('val1','val2','val3','val4');
$lastKey = end($arrTest);
foreach($arrTest as $key => $val) {
  if($val == $lastKey) {
     echo "last found";
  }
}

Comments

0

Your code is working fine : see it here : http://codepad.org/hfOFHMnc

However use "===" instead of "==" as you might encounter a bug when comparing the string with 0 , and it will echo twice.

<?php

$arrTest = array('val1','val2','val3','val4');
$arrTest['lastKey'] = 'Last Key';
print_r($arrTest);

foreach($arrTest as $key => $val) {
  if($key == 'lastKey') {       // use === here
     echo "key = $key   :: last found \n";
  }
}

Comments

0

If you want to test if an array key exists, simply use array_key_exists:

array_key_exists('lastKey', $arrTest)

You could also use isset but note that it returns false if the value associated to the key is null.

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.