我有这个嵌套数组
$lists = [
[
{"id": 1, "name": one},
{"id": 2, "name": two},
{"id": 3, "name": three},
],
[
{"id": 4, "name": four},
{"id": 5, "name": five},
{"id": 6, "name": six},
]
]
我应该怎么做才能将该数组变成一个这样的数组。
[
{"id": 1, "name": one},
{"id": 2, "name": two},
{"id": 3, "name": three},
{"id": 4, "name": four},
{"id": 5, "name": five},
{"id": 6, "name": six},
]
我尝试使用此代码进行array_merge
$numbers =[];
foreach ($lists as $list) {
$numbers = array_merge($numbers, $list);
}
但是没有用。它说参数#2不是数组。
答案 0 :(得分:3)
您可以尝试这种方式。希望对您有帮助
cmd.CommandText = "spCheckIfNameExist";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = name;
答案 1 :(得分:1)
有几个问题。初始数据是无效的JSON,但这是一个问题,我已在下面的数据中对其进行了修复。
您正在遍历在循环之前创建的空数组$lists
。在这里,我是根据JSON数据创建的,并删除了数组初始化。最后,您在哪里使用$list['numbers']
的地方,numbers
并未在任何地方定义...
$json = '[
[
{"id": 1, "name": "one"},
{"id": 2, "name": "two"},
{"id": 3, "name": "three"}
],
[
{"id": 4, "name": "four"},
{"id": 5, "name": "five"},
{"id": 6, "name": "six"}
]
]';
$lists = json_decode($json, true);
$numbers =[];
foreach ($lists as $list) {
$numbers = array_merge($numbers, $list);
}
print_r($numbers);
更新:
作为对您在问题注释中添加的内容的猜测,请尝试...
$numbers =[];
foreach ($lists['numbers'] as $list) {
$numbers = array_merge($numbers, $list);
}
print_r($numbers);