TryHarder
TryHarder

Reputation: 2777

What does [1] => 0 mean in this array?

I know this must be a fairly simple question, but I haven't managed to stumble across an answer yet.

I have the following array

$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;

When I use print_r($qid) I get the following

Array ( 
 [0] => Array ( [0] => 1 [1] => 0 ) 
 [1] => Array ( [0] => 2 ) 
 [2] => Array ( [0] => 3 ) 
 [3] => Array ( [0] => 4 )
) 

I don't understand [1] => 0

in

[0] => Array ( [0] => 1 [1] => 0 )

If someone could explain what [1] => 0 means in this array, I'd greatly appreciate it. Thanks.

EDIT: It turns out that my array was indeed different to what I had written above, because it had been modified later in the code. Thanks everyone for the great answers. I'm still reading over them all and trying to make my mind understand them (Arrays turn my mind to jello).

Upvotes: 3

Views: 9623

Answers (7)

Roland Burda
Roland Burda

Reputation: 110

I got the following result after printing out the array using print_r:

Array
(
    [0] => Array
    (
        [0] => 1
    )

    [1] => Array
    (
        [0] => 2
    )

    [2] => Array
    (
        [0] => 3
    )

    [3] => Array
    (
        [0] => 4
    )

)

I guess, you might have set a value for $gid[0][1] somewhere in your code.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 882028

That two-dimensional array is actually a one-dimensional array of arrays, which is why you're getting the nesting. The [x] => y bit simply means that index x of the array has the value y.

Now your output in this case doesn't actually match your code, since

$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
print_r($qid);

produces:

Array (
    [0] => Array ( [0] => 1 )
    [1] => Array ( [0] => 2 )
    [2] => Array ( [0] => 3 )
    [3] => Array ( [0] => 4 )
) 

If you wanted to get:

Array ( 
    [0] => Array ( [0] => 1 [1] => 0 ) 
    [1] => Array ( [0] => 2 ) 
    [2] => Array ( [0] => 3 ) 
    [3] => Array ( [0] => 4 )
)

(with the first array having two elements), you'd actually need:

$qid[0][0]=1;
$qid[0][1]=0;

$qid[1][0]=2;

$qid[2][0]=3;

$qid[3][0]=4;

print_r($qid);

Upvotes: 3

Gergo Erdosi
Gergo Erdosi

Reputation: 42063

You probably added a second item to $qid[0] somewhere ($qid[0][1] = 0). This code

$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;

outputs the the correct values for me (without [1] => 0:

Array ( [0] => Array ( [0] => 1 ) [1] => Array ( [0] => 2 ) [2] => Array ( [0] => 3 ) [3] => Array ( [0] => 4 ) ) 

Upvotes: 2

hafichuk
hafichuk

Reputation: 10781

Simply put, you have a numerically indexed multidimensional array. http://php.net/manual/en/language.types.array.php should have all the information you need to read up on this.

As to why you have the [1] => 0, you'll need to look a little deeper into your code to see where it gets assigned.

Upvotes: 1

Saiyam Patel
Saiyam Patel

Reputation: 1161

[1] => 0

in this simple way we can say that 1 is your array key and 0 is value for the 1 key 0 is store at the 1 key of the array

thanks

Upvotes: 1

James P. Wright
James P. Wright

Reputation: 9131

It means that your index 0 in the original Array contains another Array of 2 items.
Specifically [1] => 0 means that the 2nd item of the "child" Array contains the number 0.

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270677

[1] => 0 denotes an array element with the value 0.

The numbers in [] are array keys. So [1] is the second element of a numerically indexed array, (which starts with [0]), and the value of the second element ([1]) is 0.

PHP uses => as an operator to relate array keys/indices to their values.

So an overall explanation of this structure:

Array ( 
 [0] => Array ( [0] => 1 [1] => 0 ) 
 [1] => Array ( [0] => 2 ) 
 [2] => Array ( [0] => 3 ) 
 [3] => Array ( [0] => 4 )
) 

The outer array is a numerically indexed array, and each of its elements is a sub-array. The first of them ([0]) is an array containing 2 elements, while the rest of them ([1] through [3]) are arrays containing only one single element.

Upvotes: 4

Related Questions