json_decode() 当有多个结果时

json_decode() when there are multiple results

我想弄清楚如何从 json 结果中获取 SKU(以及 ID)。当只有一组称为 SKU 的值时,我可以执行此操作,但我花了数小时试图了解当有多个值时如何执行此操作。

下面是我的 json returns

{
    "variants": [
        {
            "id": 6852445,
            "name": "Ikan VK7i 7\" LCD Monitor for Sony L with sun hood",
            "sku": "VK7i-S-SHX7",
        },
        {
            "id": 6852388,
            "name": "ikan Flyweight DSLR",
            "sku": "ELE-FLWDSLR",
        },
        {
            "id": 6838367,
            "name": "Atomos Sun Hood for Ninja/Ninja-2 including Double Adapter",
            "sku": "AO-ATOMSUN001",
        },
                ]
}

我目前有这个代码(我是新人)。我想要做的是让 sku 和 ID 都成为变量,但我只是碰壁了。目前我得到

警告:第 15

行 /home/pearingc/public_html/returns/test2.php 中为 foreach() 提供的参数无效

<?php

$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Bearer *my token details* ")
    )
);

$url = "http://api.tradegecko.com/variants/";
$data = file_get_contents($url, false, $context);

$json = json_decode($data, true);

$product = $json{'variant'}->{'sku'};
foreach ($product['sku'] as $sku) {
print $sku;
}

?>

编辑:这就是 var_dump($json);给我

object(stdClass)#1 (2) { ["variants"]=> array(100) { [0]=> object(stdClass)#2 (42) { ["id"]=> int(6852445) ["created_at"]=> string(24) "2015-05-20T10:09:09.629Z" ["updated_at"]=> string(24) "2015-05-20T10:10:40.351Z" ["product_id"]=> int(1991122) ["default_ledger_account_id"]=> NULL ["buy_price"]=> string(5) "404.0" ["committed_stock"]=> string(1) "0" ["incoming_stock"]=> string(1) "0" ["composite"]=> bool(true) ["description"]=> NULL ["is_online"]=> bool(false) ["keep_selling"]=> bool(false) ["last_cost_price"]=> NULL ["manage_stock"]=> bool(true) ["max_online"]=> NULL ["moving_average_cost"]=> NULL ["name"]=> string(49) "Ikan VK7i 7" LCD Monitor for Sony L with sun hood" ["online_ordering"]=> bool(false) ["opt1"]=> NULL ["opt2"]=> NULL ["opt3"]=> NULL ["position"]=> int(6) ["product_name"]=> string(33) "ikan 7" HDMI Monitor W/ IPS Panel" ["product_status"]=> string(6) "active" ["product_type"]=> string(8) "Monitors" ["retail_price"]=> NULL ["sellable"]=> bool(true) ["sku"]=> string(11) "VK7i-S-SHX7" ["status"]=> string(6) "active" ["stock_on_hand"]=> string(1) "0" ["supplier_code"]=> NULL ["taxable"]=> bool(true) ["upc"]=> NULL ["weight"]=> NULL ["wholesale_price"]=> NULL ["image_ids"]=> array(0) { } ["variant_prices"]=> array(1) { [0]=> object(stdClass)#3 (2) { ["price_list_id"]=> string(3) "buy" ["value"]=> string(5) "404.0" } } ["locations"]=> array(1) { [0]=> object(stdClass)#4 (6) { ["location_id"]=> int(16377) ["stock_on_hand"]=> string(1) "0" ["committed"]=> string(1) "0" ["incoming"]=> NULL ["bin_location"]=> NULL ["reorder_point"]=> NULL } } ["prices"]=> object(stdClass)#5 (1) { ["buy"]=> string(5) "404.0" } ["stock_levels"]=> object(stdClass)#6 (1) { ["16377"]=> string(3) "0.0" } ["committed_stock_levels"]=> object(stdClass)#7 (1) { ["16377"]=> string(3) "0.0" } ["incoming_stock_levels"]=> object(stdClass)#8 (0) { } } 

我认为你格式不正确JSON。注意:我在 sku 之后删除了逗号。试试这个:

$data = '{
"variants": [
    {
        "id": 6852445,
        "name": "Ikan VK7i 7\" LCD Monitor for Sony L with sun hood",
        "sku": "VK7i-S-SHX7"
    },
    {
        "id": 6852388,
        "name": "ikan Flyweight DSLR",
        "sku": "ELE-FLWDSLR"
    },
    {
        "id": 6838367,
        "name": "Atomos Sun Hood for Ninja/Ninja-2 including Double Adapter",
        "sku": "AO-ATOMSUN001"
    }
]
}';

之后:

$json = json_decode($data);
foreach ($json->variants as $row) {
    print $row->sku;
}
<?php

$json = '{
    "variants": [
        {
            "id": 6852445,
            "name": "Ikan VK7i 7\" LCD Monitor for Sony L with sun hood",
            "sku": "VK7i-S-SHX7"
        },
        {
            "id": 6852388,
            "name": "ikan Flyweight DSLR",
            "sku": "ELE-FLWDSLR"
        },
        {
            "id": 6838367,
            "name": "Atomos Sun Hood for Ninja/Ninja-2 including Double Adapter",
            "sku": "AO-ATOMSUN001"
        }
    ]
}';

foreach(json_decode($json,true)['variants'] as $item) {
  echo $item['sku'] . "<br />";
}
?>

压缩了一点。