【发布时间】:2019-11-04 17:01:17
【问题描述】:
我有以下结构:
{
"produto_id" : 54,
"descricao_id" : 25,
"contas" : [
{
"marketplace_id" : 8,
"contas_ids" : [
6, 8, 9
]
},
{
"marketplace_id" : 9,
"contas_ids" : [
44, 100
]
}
]
}
我想得到一个包含所有“contas_ids”的数组,如下所示:
[6, 8, 9, 44, 100]
我已经尝试过array_map,但我不得不使用这么多。使用 array_column 我实现了一些接近但它将输出分成几个数组。
$ids = array_column($contas, 'contas_ids');
使用“dd”我明白了。
array:2 [
0 => array:3 [
0 => 6
1 => 8
2 => 9
]
1 => array:2 [
0 => 44
1 => 100
]
]
有人可以帮我生成一个包含所有“contas_id”的数组吗?
【问题讨论】:
-
@ggorlen 在我看来是有效的 JSON。
-
你是对的。即便如此,尚不清楚
$contas是什么——它是完整的结构还是只是子数组?添加解析代码作为minimal reproducible example 的一部分会很有帮助。 -
@ggorlen 同意,看起来它只是
contas数组。这就是我在回答中的假设。 -
您提到使用 dd,这是在 Laravel 应用程序中吗?如果该数据来自查询,则可能有更好的方法来执行此操作。
-
为什么这个json无效?我从一个发布请求中得到这个 json。
标签: php