PHP多维数组JSON [英] PHP multidimensional array to JSON
问题描述
所以我尝试找出最佳的方式来获得MySQL表的数据变成一个多维PHP数组或多维数组转换成JSON字符串。
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.
基本上什么即时试图做的是有一个PHP包括返回JSON字符串,所以我可以通过它进行迭代。我需要有多个值的单个密钥,所以我不会100%地肯定,即时通讯在正确的方向前进。
Essentially what im trying to do is have a php include that returns the JSON string so i can iterate through it. I am needing a single key with multiple values, so im not 100% sure that im headed in the right direction.
我要多个值分配给同一键,例如:
I want to assign multiple values to the same key, for example:
[{"key1": "package1", "package2", "package3"}, {"key2": "package1", "package2", "package3", "package4"}]
我认为是不会工作的权利?因为我没有任何类型指数的呢?
I think that is not going to work right? Because i dont have any type of index's?
推荐答案
这是无效的JSON。你正在寻找的结构是这样的:
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..
]
在PHP的一面,你需要这样的:
At the PHP side, you would need something like:
- 对于每一行从MySQL中取出
-
$改编[$关键]
=<新数组> - 每个包:
- 追加包
$改编[$关键]
- 追加包
-
- For every row fetched from MySQL
$arr[$key]
= <new array>- for each package:
- append package to
$arr[$key]
- append package to
这篇关于PHP多维数组JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!