Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following array in php

Array
(
    [0] => Array
        (
            [id] => 0
            [name] => name1
            [short_name] => n1
        )

    [2] => Array
        (
            [id] => 2
            [name] => name2
            [short_name] => n2
        )
)

I want the json to be in the following format

[{"id":0, "name":"name1", "short_name":"n1"},{"id":2, "name":"name2", "short_name":"n2"}]

how do i achieve it in php? when i use json_encode i am getting the following instead,

{
     0:{"id":0, "name":"name1", "short_name":"n1"},
     2:{"id":2, "name":"name2", "short_name":"n2"}
}

which is an object instead of an array,

EDIT:

I want to use the following array with knockout.js

Array
(
    [0] => Array
        (
            [id] => 0
            [name] => name1
            [short_name] => n1
        )

    [2] => Array
        (
            [id] => 2
            [name] => name2
            [short_name] => n2
        )
)

and i am unable to loop.

Edit:

after your help i figured out my array wasn't sequential which resulted in an object instead of an array,

Array
(
    [0] => Array
        (
            [id] => 0
            [name] => name1
            [short_name] => n1
        )

    [2] => Array
        (
            [id] => 2
            [name] => name2
            [short_name] => n2
        )
)

the following works,

Array
(
    [0] => Array
        (
            [id] => 0
            [name] => name1
            [short_name] => n1
        )

    [1] => Array
        (
            [id] => 2
            [name] => name2
            [short_name] => n2
        )
)

I could achieve it finally here http://jsfiddle.net/vZdJz/4/

Thanks for all your help guys!!!

share|improve this question
6  
You want the json to be invalid? – Esailija Jun 25 '12 at 19:08
["id":0, "name":"name1", "short_name":"n1"] is not valid JSON. Neither is {0:. – cdhowie Jun 25 '12 at 19:09
Want you want is neither an array in php nor a json in javascript – Blaster Jun 25 '12 at 19:09
but [["id":0, "name":"name1", "short_name":"n1"],["id":1, "name":"name2", "short_name":"n2"]] should be theorically possible according to the norm, no ? – dystroy Jun 25 '12 at 19:10
@dystroy theoretically, if you invent a new data encoding language – Juan Mendes Jun 25 '12 at 19:12
show 11 more comments

2 Answers

up vote 4 down vote accepted

Array in JSON are indexed array only, so the structure you're trying to get is not valid Json/Javascript.

PHP Associatives array are objects in JSON, so unless you don't need the index, you can't do such conversions.

If you want to get such structure you can do:

$indexedOnly = array();

foreach ($associative as $row) {
    $indexedOnly[] = array_values($row);
}

json_encode($indexedOnly);

Will returns something like:

[
     [0, "name1", "n1"],
     [1, "name2", "n2"],
]
share|improve this answer
Thanks for giving me the clue of indexed array :) the problem was i indexed it in such a way that the id was also the id of the array instead of a sequential id and json_encode didn't pick it up as an array but as in object instead because the array wasn't sequential its fixed now thanks for all your help guys :) – peplamb Jun 25 '12 at 19:30
Not true. Some libraries do encode associative arrays as arrays with index as one of element properties. – Gustavo Pinent Jun 18 at 1:11

The format you want is not a valid JSON, because there are no associative arrays in JavaScript. I tried to encode the same array as you and it gave me:

[
    {"id":0,"name":"name1","short_name":"n2"},
    {"id":1,"name":"name2","short_name":"n2"}
]

which is correct. (PHP 5.3.10)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.