PHP使用字典遍历数组

PHP使用字典遍历数组,php,arrays,sorting,dictionary,Php,Arrays,Sorting,Dictionary,嗨,我是php新手,我有一个数组,看起来像下面的代码 [ { "amount" : "204", "order" : "15", "customer": "12"}, { "amount" : "208", "order" : "17", "customer": "18"},{ "amount" : "300", "order" : "15", "customer": "19"} ] 如何迭代并获取所有金额?尝试使用foreach,结果在我使用的地方出现了无效的参数 $xabo = [

嗨,我是php新手,我有一个数组,看起来像下面的代码

 [ { "amount" : "204", "order" : "15", "customer": "12"}, { "amount" : "208", "order" : "17", "customer": "18"},{ "amount" : "300", "order" : "15", "customer": "19"} ]
如何迭代并获取所有金额?尝试使用foreach,结果在我使用的地方出现了无效的参数

   $xabo = [ { "amount" : "204", "order" : "15", "customer": "12"}, { "amount" : "208", "order" : "17", "customer": "18"},{ "amount" : "300", "order" : "15", "customer": "19"} ] 
   foreach($xabo as $denge){
   print_r $denge['amount'];
   } 

这是一个JSON编码的数组。您需要将其定义为字符串并对其进行解码。对于打印,您还需要括号

 $xabo_str = '[ { "amount" : "204", "order" : "15", "customer": "12"}, { "amount" : "208", "order" : "17", "customer": "18"},{ "amount" : "300", "order" : "15", "customer": "19"} ]';
 $xabo = json_decode($xabo_str, true);
 foreach($xabo as $denge){
   print_r($denge['amount']);
 } 
json_decode中的true确保只获取数组。通过这种方式,您可以使用
数组['key']
语法访问密钥。

可能重复的“JSON编码数组”的可能重复项这不是编码数组。它是一个简单的JSON字符串,而不是数组。如果您实际使用json_encode()将其转换回来,那么它是经过编码的。