Monday, February 2

json_encode 'php array' to a 'json array'

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:

<?php
    $indexedOnly = array();
    
    foreach ($associative as $row) {
        $indexedOnly[] = array_values($row);
    }
    
    json_encode($indexedOnly);
?>

Will returns something like:

[
     [0, "name1", "n1"],
     [1, "name2", "n2"],
]

No comments:

Post a Comment