转换数组以复制关联/顺序json

时间:2016-06-20 19:13:45

标签: php arrays json

我试图通过使用json_encode转换php数组来复制这个json:

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

使用以下PHP数组:

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

如果我只是在数组中得分,PHP正确地没有为数组分配一个键:

"sort" : [
    "_score"
]

但是当我将"age"=>"desc"混合到混合中时,json_encode无法正确编码_score

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

1 个答案:

答案 0 :(得分:2)

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