PHP循环数组和内部数组数据

PHP循环数组和内部数组数据,php,arrays,Php,Arrays,从这个示例数组中获取条形码和sku有点问题 { "title": "This is the title", "variants": [{ "barcode": "123456789", "sku": "sku1" }] } PHP 试试这个,希望它能起作用: foreach ($products as $product) { $products[] = Array( "title" => $product["title"], //

从这个示例数组中获取条形码和sku有点问题

{
  "title": "This is the title",
  "variants": [{
    "barcode": "123456789", 
    "sku": "sku1"
  }]
}
PHP


试试这个,希望它能起作用:

foreach ($products as $product) {

     $products[] = Array(
        "title" => $product["title"], // product title
        "barcode" => $product["variants"][0]["barcode"], // product barcode
        "sku" => $product["variants"][0]["sku"] // product SKU
     );
  }

您的数据看起来像JSON,因此需要首先将其转换为PHP数组:

$products = json_decode($data, true);
现在您可以这样迭代:

$product_array = array();
foreach ($products as $product) {

    $product_array[] = array(
       "title" => $product["title"], // product title
       "barcode" => $product["variants"][0]["barcode"], // product barcode
       "sku" => $product["variants"][0]["sku"] // product SKU
    );
}
请注意在
foreach
循环中添加的
[0]
,以及
$product\u数组
数组(因为您正在覆盖循环本身中的
$products
数组)。
[0]
用于获取数组
变体的第一个元素,因为可以有更多。您可以添加另一个
foreach
循环来迭代它们

还要注意,在使用
json\u decode
之后,您已经拥有了一个完整的数组。您只需运行以下命令即可检查:

var_dump(json_decode($data, true));
其中
$data
包含您提到的JSON数据

更新的答案中包含循环:

$product_array = array();

foreach ($products as $product) {
    // I'm using the name $item, if I used $product it would overwrite the variable in the loop
    $item = array(
        "title" => $product["title"], // product title
        "variants" => array() // for now an empty array, we will fill it in the next step
    );

    // now we fill the empty arrays inside the array above, by iterating all variants
    foreach($product["variants"] as $variant) {
        $item["variants"][] = array(
            "barcode" => $variant["barcode"], // product barcode
            "sku" => $variant["sku"] // product SKU
        );
    }
    $product_array[] = $item; // now we add the item with all the variants to the array with all the products
}

var_dump($product_array); // show the results
试试这个代码

$a = '{
"title": "This is the title",
"variants": [{
  "barcode": "123456789", 
  "sku": "sku1"
 }]
}';
$products = json_decode($a, TRUE);
$products = Array(
   "title" => $products['title'], // product title
   "barcode" => $products['variants'][0]["barcode"], // product barcode
   "sku" => $products['variants'][0]["sku"] // product SKU
);

您是否正确解码了json?重复的是,它完全有效,这只是我刚才在这里写的一个示例数据,有很多数据需要粘贴,但需要获取数组的子数据您正在将新值分配给相同的
$products
数组。我几乎可以肯定,首先打开错误报告并检查是否出现了一些错误或警告,这不是有意的:
ini\u set(“display\u errors”,1);错误报告(E_全部)
还可以使用
print\r($products)查看阵列的结构查看你在那里得到了什么。现在你给我们展示了一个数组,你可以在其中循环
title
variants
。是的,这就成功了,我想尝试一下[0],但我只想做第一个,但它确实像预期的那样循环,谢谢Mate,我接受了,但10分钟内无法接受,但我刚刚做了:)我对PHP有点陌生,你能给我举个例子,这样就不必使用[0]和另一个循环了吗?虽然这样做很有效,而且我的数据显示得很好。。。如果有更好的方法,我想学习更好的方法:)我已经从PHP框架json了数据,我想。。。好吧,我假设当加载页面加载时一切都很好。当然,把它添加到我的答案中!
$a = '{
"title": "This is the title",
"variants": [{
  "barcode": "123456789", 
  "sku": "sku1"
 }]
}';
$products = json_decode($a, TRUE);
$products = Array(
   "title" => $products['title'], // product title
   "barcode" => $products['variants'][0]["barcode"], // product barcode
   "sku" => $products['variants'][0]["sku"] // product SKU
);