json_encode PHP array as JSON array not JSON object

PhpJson

Php Problem Overview


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 to JSON encode it as a JSON array, producing a string like the following:

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

But when I call json_encode on this array, I get the following:

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

Which is an object instead of an array.

How can I get json_encode to encode my array as an array, instead?

Php Solutions


Solution 1 - Php

See Arrays in RFC 8259 The JavaScript Object Notation (JSON) Data Interchange Format: >An array structure is represented as square brackets surrounding zero or more values (or elements). Elements are separated by commas. > >array = begin-array [ value *( value-separator value ) ] end-array

You are observing this behaviour because your array is not sequential - it has keys 0 and 2, but doesn't have 1 as a key.

Just having numeric indexes isn't enough. json_encode will only encode your PHP array as a JSON array if your PHP array is sequential - that is, if its keys are 0, 1, 2, 3, ...

You can reindex your array sequentially using the array_values function to get the behaviour you want. For example, the code below works successfully in your use case:

echo json_encode(array_values($input)).

Solution 2 - Php

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"],
]

Solution 3 - Php

>json_decode($jsondata, true);

true turns all properties to array (sequential or not)

Solution 4 - Php

Try this,

<?php
$arr1=array('result1'=>'abcd','result2'=>'efg'); 
$arr2=array('result1'=>'hijk','result2'=>'lmn'); 
$arr3=array($arr1,$arr2); 
print (json_encode($arr3)); 
?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser677607View Question on Stackoverflow
Solution 1 - PhpNguyen Van VinhView Answer on Stackoverflow
Solution 2 - PhpBoris GuéryView Answer on Stackoverflow
Solution 3 - PhpRobert SinclairView Answer on Stackoverflow
Solution 4 - PhpY0GiView Answer on Stackoverflow