Php 访问某些JSON元素

Php 访问某些JSON元素,php,json,foreach,Php,Json,Foreach,如何在PHP中从Shopify JSON访问这些元素 JSON: 这只是对象的一个片段。如何访问每个值并将其分配给名称?例如,这个$req['account_id']将等于该名称标记的值。这就是我试图这样做但不起作用的方式: foreach ($order['note_attributes'] as $attributes) { $req = array(); $req[$attributes->nam

如何在PHP中从Shopify JSON访问这些元素

JSON:

这只是对象的一个片段。如何访问每个值并将其分配给名称?例如,这个$req['account_id']将等于该名称标记的值。这就是我试图这样做但不起作用的方式:

foreach ($order['note_attributes'] as $attributes) {
       
       
        $req = array();
        
 

        $req[$attributes->name] = $attributes[$attributes->value];

    
        }

然后我想回显$req['account\u id'],这将=xxxxxxxxx@gmail.com但是它没有任何建议或修复功能?

当迭代对象属性时,一种方法是使用(如您所用)

例如:

$attributes = array();
.
.
foreach($note_attributes as $note_attribute) {
  $key = $note_attribute['name'];
  $value = $note_attribute['value'];
  $attributes[$key] = $value;
}
结构应该是键值对。即

attributes [
  "account1234" => "abc@xxx.com",
  "account_password" => "asdasdasd"
]
希望这有帮助

更新:如Roopendra所述,使用带有返回关联数组的TRUE标志,否则使用stdClass。

尝试一下,它会将json转换为php变量。
$attributes = array();
.
.
foreach($note_attributes as $note_attribute) {
  $key = $note_attribute['name'];
  $value = $note_attribute['value'];
  $attributes[$key] = $value;
}
attributes [
  "account1234" => "abc@xxx.com",
  "account_password" => "asdasdasd"
]