0

I have a little problem with the key of an array. The array looks like this:

Array
(
    [1] => Array
        (
            ["question"] => test question 1
            ["open_response"] => 1
        )

    [2] => Array
        (
            ["question"] => test question 2
            ["yes_no"] => 1
        )

)

But for some reason whenever I try to access $data['1']['question'] it tells me that question is not an index. I'm a little confused about this since it should be a key but is not, how can I fix this? or how can i access it?

1
  • are you sure that you exactly defined that array and then you typed $data['1']['question'] ? can you show the full code?! Commented Apr 8, 2013 at 19:30

2 Answers 2

5

You want:

$data[1]['question']

Not:

$data['1']['question']

Edit:

My answer doesn't solve his problem, rather it helped him find the actual issue. The two snippets above are exactly the same thing because PHP will cast string keys into integers if the string is a valid integer. IMO, its confusing as hell. If I key my array with a string, dag`nammit it should be keyed with a string even if that string can also be parsed into an int!

The relevant documentation can be found here:

The key can either be an integer or a string. The value can be of any type.

Additionally the following key casts will occur:

  1. Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
  2. Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
  3. Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
  4. Null will be cast to the empty string, i.e. the key null will actually be stored under "".
  5. Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.
Sign up to request clarification or add additional context in comments.

10 Comments

Are you sure? I tested it and $data['1']['question'] returns text question 1.
Thanks man, sometimes we coders need an extra eye... such a stupid mistake, well I've been coding since last night, I think I need some rest, Thanks man :)
test this $data[1]['question'] = 'test';echo $data['1']['question'];
@TheManiac If they are not the same, how come array(0=>"zero", 1=>"one", "1"=>"one_string") creates a 2 elements array instead of 3? (I am not trying to prove you wrong, I am just confused)
@TheManiac Wow I never realized that before. Thanks for explaining!
|
0

Where are your array providing from? Array indexes may have nonvisible (by your editor, browser etc) chars like backspace or null (\0) character. You can not see theese chars on var_dump.

Look this example : code :

<pre>

<?php
$a = array(
    "\0question\0" => "test question 1",
    "question\0" => "test question 2",
    "\0question" => "test question 3",
    "question" => "test question 4"
   );

var_dump($a);
?>

Output :

array(4) {
  ["question"]=>
  string(15) "test question 1"
  ["question"]=>
  string(15) "test question 2"
  ["question"]=>
  string(15) "test question 3"
  ["question"]=>
  string(15) "test question 4"
}

you can use some array functions like : array_values, array_map to rebuil and validate your array.

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.