【发布时间】:2018-06-28 14:11:23
【问题描述】:
有一个 json 存储在 数据库 中,而 keys 是未知的。
我想像这样打印它们
array_key : amount 用于第一个数组 key => cigratte_flake, Amount => 2997
这是我解码的 json
stdClass Object
(
[cigratte_flake] => Array
(
[0] => stdClass Object
(
[Price] => 999
[Quantity] => 3
[Amount] => 2997
)
)
[wrestling_board] => Array
(
[0] => stdClass Object
(
[Price] => 400
[Quantity] => 2
[Amount] => 800
)
)
[flex_board_naga_dhaga] => Array
(
[0] => stdClass Object
(
[Price] => 300
[Quantity] => 2
[Amount] => 600
)
)
[total] => Array
(
[0] => stdClass Object
(
[Price] =>
[Quantity] =>
[Amount] => 4397
)
)
)
这是我的原始 json:
{"cigratte_flake":[{"Price":"999","Quantity":"3","Amount":"2997"}],"wrestling_board":[{"Price":"400", "数量":"2","数量":"800"}],"flex_board_naga_dhaga":[{"价格":"300","数量":"2","数量":"600"}], "total":[{"Price":null,"Quantity":null,"Amount":"4397"}]}
我知道下面的代码,但不知道如何在 foreach 循环中应用,其他解决方案最赞赏
reset($a);
$first_key = key($a);
请帮助我提前谢谢!!!
【问题讨论】:
-
请提供您当前的用于解码和解释 JSON 的 PHP 代码,以便我们了解您遇到的问题 :-)
-
@PhilippMaurer,
array key将是未知的,但它的values将是Price,Quantity,Price -
这是我的代码
$d = \DB::select('SELECT sqd.quotation_data,pi.* FROM sent_quotation_data sqd INNER JOIN performa_invoice pi on sqd.customer_id = pi.customer_id'); $d = collect($d); $j = json_decode($d[0]->quotation_data);