Cant get last array value

I dont understand why my array gets cut in loop?

Array
(
    [0] => Array
        (
            [name] => order
            [value] => asd
        )

    [1] => Array
        (
            [name] => item
            [value] => aa
        )

    [2] => Array
        (
            [name] => quant
            [value] => 5
        )

    [3] => Array
        (
            [name] => price
            [value] => 20
        )

)

My php code with for loop:

for($i = 0; $i < count($json_array); $i++)
{
    echo $json_array[$i]['name'];
}

The result I'm getting is: orderitemquant but why last value price is gone? What is wrong with this code?

Upvotes: 0

Views: 129

Answers (2)

C.A. Vuyk
C.A. Vuyk

Reputation: 1085

Try this, off by one error:

for($i = 0; $i =< count($json_array); $i++)
{
    echo $json_array[$i]['name'];
}

Upvotes: -1

Sergey Telshevsky
Sergey Telshevsky

Reputation: 12197

Have you tried using foreach? In my opinion it suits better for iterating your array.

foreach($json_array as $sub_array) {
    echo $sub_array['name'];
}

Upvotes: 2

Related Questions