Jared Dunham Jared Dunham - 5 months ago 10
JSON Question

Converting an array to replicate an associative/sequential json

I'm trying to replicate this json by converting a php array with

json_encode
:

"sort" : [
{ "age" : "desc" },
"_score"
]


With the following PHP array:

$json_doc['sort'] = array(
"_score",
"age"=>"desc"
);


If I just have the score in the array, PHP properly does not assign a key to the array:

"sort" : [
"_score"
]


But when I mix the
"age"=>"desc"
into the mix,
json_encode
does not encode the
_score
correctly:

"sort": {
"0": "_score",
"age": "desc"
}

Answer
$json_doc['sort'] = Array(
      "_score",
      Array('age' => 'desc')
    );
Comments