How do I `json_encode()` keys from PHP array?

前端 未结 4 1667
鱼传尺愫
鱼传尺愫 2020-12-03 10:59

I have an array which prints like this

Array ( [0] => 1691864 [1] => 7944458 [2] => 9274078 [3] => 1062072 [4] => 8625335 [5] => 8255371 [6         


        
4条回答
  •  温柔的废话
    2020-12-03 11:36

    You can force that json_encode uses an object although you’re passing an array with numeric keys by setting the JSON_FORCE_OBJECT option:

    json_encode($thearray, JSON_FORCE_OBJECT)
    

    Then the returned value will be a JSON object with numeric keys:

    {"0":1691864,"1":7944458,"2":9274078,"3":1062072,"4":8625335,"5":8255371,"6":5476104,"7":6145446,"8":7525604,"9":5947143}
    

    But you should only do this if an object is really required.

提交回复
热议问题