PHP multidimensional array to JSON

后端 未结 2 1730
我在风中等你
我在风中等你 2020-12-20 06:17

So im trying to figure out the best way to get MySql table data into either a multidimensional PHP array or convert that multidimensional array into a json string.

E

2条回答
  •  既然无缘
    2020-12-20 06:38

    That is not valid JSON. The structure you are looking for would be something like:

    [
     {"key1": ["package1", "package2", "package3"]},
     {"key2": ["package1", "package2", "package3", "package4"}]
              ^ An array as the value to the key "key1", "key2", etc..
    ]
    

    At the PHP side, you would need something like:

    1. For every row fetched from MySQL
      • $arr[$key] =
      • for each package:
        • append package to $arr[$key]
    2. echo out json_encode($arr)

提交回复
热议问题