1

I'm not able to figure out what the issue is here. I have an associative array with predefined indexes and when trying to access one of the indexes, I'm getting the undefined index error, here's the code,

        if(!isset($score_value[$index])){
            echo $index . ' isnt in array: <br/>';
            print_r($score_value);
            exit;
        }
        print_r($score_value[$index]);

The output was this:

pi_cholesterol isn't in array:

Array ( 
    [pi_overall_health] => Array ( 
        [4] => 4 [1] => 1 [2] => 4 [3] => 1 
    ) 
    [pi_bmi] => Array ( 
        [Healthy Weight] => 4 [Obese] => 3 [Overweight] => 3 
    ) 
    [pi_cholesterol] => Array ( 
        [Yes - its level is too high] => 6 [Yes - its level is ok] => 3 [No] => 1 
    ) 
)

As you can see pi_cholesterol is an index in the array but for some reason isset() is flagging it as not being in the array, same thing happens if I try with array_key_exists(). There might be some simple thing I'm overlooking but I can't see it.

Any suggestions welcome!

Nirav Madariya
  • 1,470
  • 2
  • 24
  • 37
Ross
  • 112
  • 1
  • 17
  • what is $index == ? – lazyCoder May 11 '17 at 11:18
  • In this case $index = 'pi_cholesterol' – Ross May 11 '17 at 11:19
  • @Ross So the array you included in the question is the output from `print_r($score_value);`? – Alon Eitan May 11 '17 at 11:20
  • Yes the array in the question is the output from `print_r($score_value);` – Ross May 11 '17 at 11:22
  • loop for that array and var_dump each key of it – Vidish Purohit May 11 '17 at 11:22
  • You might have a typo somewhere, a sneaky whitespace can make man go mad. Could you do what @VidishPurohit suggested and hash every key, then compare it to the hash of your `$index`. It could literally be a sneaky space or tab that causes this problem. – Mjh May 11 '17 at 11:25
  • Make sure `$index` does not contain padding spaces. – axiac May 11 '17 at 11:26
  • check this [link](http://sandbox.onlinephpfunctions.com/code/7aa200531de6bfde8e1c47181a8690afc496adda) its working. check with $index = trim($index); before checking condition. – Rahul May 11 '17 at 11:27
  • Key:string(17) "pi_overall_health" Value:array(4) { [4]=> int(4) [1]=> int(1) [2]=> int(4) [3]=> int(1) } Key:string(6) "pi_bmi" Value:array(3) { ["Healthy Weight"]=> int(4) ["Obese"]=> int(3) ["Overweight"]=> int(3) } Key:string(14) "pi_cholesterol" Value:array(3) { ["Yes - its level is too high"]=> int(6) ["Yes - its level is ok"]=> int(3) ["No"]=> int(1) } – Ross May 11 '17 at 11:27
  • Is the result of `foreach ($score_value as $key => $value){ echo '
    Key:'; var_dump($key); echo '
    Value:'; var_dump($value); }`
    – Ross May 11 '17 at 11:28
  • @Ross it should work because it's working for my case check here https://3v4l.org/nrCnj – lazyCoder May 11 '17 at 11:28
  • @Ross are you using any loop please update your full code – lazyCoder May 11 '17 at 11:32
  • @Ross Can you update your full code as? – Vidish Purohit May 11 '17 at 11:40
  • what about `$index = trim($index);` (@mjh stated `a sneaky whitespace` somewhere ?) EDIT: too late, already own-answered by OP... sorry – OldPadawan May 11 '17 at 11:41

1 Answers1

0

Sorry my own stupid mistake, $index was being overwritten in another place in the code and was being appended with a trailing whitespace.

Mjh, axiac and rahul_m suggesting to use trim() helped me solve this.

Ross
  • 112
  • 1
  • 17