如何从 json 获取数组并将其转换为 foreach

How to get array from json and convert it to foreach

我有这样的 JSON 输出

 $json =  { "MOBILE_NETWORK": { "MTN": [ { "ID":"01","PRODUCT": [ {"PRODUCT_CODE":"1000","PRODUCT_ID":"1000","PRODUCT_NAME":"1GB","PRODUCT_AMOUNT":"335"}, {"PRODUCT_CODE":"2000","PRODUCT_ID":"2000","PRODUCT_NAME":"2GB","PRODUCT_AMOUNT":"670"}, {"PRODUCT_CODE":"5000","PRODUCT_ID":"5000","PRODUCT_NAME":"5GB","PRODUCT_AMOUNT":"1675"} ] } ], "Glo": [ { "ID":"02","PRODUCT": [ {"PRODUCT_CODE":"500","PRODUCT_ID":"500.01","PRODUCT_NAME":"1.6GB/1.8GB","PRODUCT_AMOUNT":"920"}, ............... ] } ] } }

 $json = file_get_contents("https:......");

                                 if (!empty($result = json_decode($json, 1))) {
                                    $MOBILE_NETWORK = $result['MOBILE_NETWORK'];
                                      var_dump($MOBILE_NETWORK);
                                           }

                                           $network = $_GET["data-network"];

                                           if($network = "MTN"){
                                           foreach ($MOBILE_NETWORK["MTN"][0]["PRODUCT"][0] as $k => $product) {
    if ($k === 'PRODUCT_NAME') { echo '<option value="'.$product.'">'."$product"; }
}
}

if($network = "GLO"){
foreach ($MOBILE_NETWORK['Glo'][0]["PRODUCT"][0] as $k => $product) {
    if ($k === 'PRODUCT_NAME') { echo '<option value="'.$product.'">'."$product"; }
}

}

if($network = "ETISALAT"){
foreach ($MOBILE_NETWORK['9mobile'][0]["PRODUCT"][0] as $k => $product) {
    if ($k === 'PRODUCT_NAME') { echo '<option value="'.$product.'">'."$product"; }
}

}

if($network = "AIRTEL"){
foreach ($MOBILE_NETWORK['Airtel'][0]["PRODUCT"][0] as $k => $product) {
    if ($k === 'PRODUCT_NAME') { echo '<option value="'.$product.'">'."$product"; }
}

}

但是我一直看到一组数组,请问如何根据$network得到FOREACH (GET)值?

您可以将其作为数组获取:

<?php

    $json = '{ "MOBILE_NETWORK": { "MTN": [ { "ID":"01","PRODUCT": [ {"PRODUCT_CODE":"1000","PRODUCT_ID":"1000","PRODUCT_NAME":"1GB","PRODUCT_AMOUNT":"335"}, {"PRODUCT_CODE":"2000","PRODUCT_ID":"2000","PRODUCT_NAME":"2GB","PRODUCT_AMOUNT":"670"}, {"PRODUCT_CODE":"5000","PRODUCT_ID":"5000","PRODUCT_NAME":"5GB","PRODUCT_AMOUNT":"1675"} ] } ], "Glo": [ { "ID":"02","PRODUCT": [ {"PRODUCT_CODE":"500","PRODUCT_ID":"500.01","PRODUCT_NAME":"1.6GB/1.8GB","PRODUCT_AMOUNT":"920"}, {"PRODUCT_CODE":"1600","PRODUCT_ID":"1600.01","PRODUCT_NAME":"1.6GB/1.8GB","PRODUCT_AMOUNT":"920"}, {"PRODUCT_CODE":"3750","PRODUCT_ID":"3750.01","PRODUCT_NAME":"4GB/5GB","PRODUCT_AMOUNT":"1840"}, {"PRODUCT_CODE":"5000","PRODUCT_ID":"5000.01","PRODUCT_NAME":"5.75GB/7.2GB","PRODUCT_AMOUNT":"2300"}, {"PRODUCT_CODE":"6000","PRODUCT_ID":"6000.01","PRODUCT_NAME":"7GB/8.75GB","PRODUCT_AMOUNT":"2760"}, {"PRODUCT_CODE":"8000","PRODUCT_ID":"8000.01","PRODUCT_NAME":"10GB/12.5GB","PRODUCT_AMOUNT":"3680"}, {"PRODUCT_CODE":"12000","PRODUCT_ID":"12000.01","PRODUCT_NAME":"12.5GB/15.6GB","PRODUCT_AMOUNT":"4600"}, {"PRODUCT_CODE":"16000","PRODUCT_ID":"16000.01","PRODUCT_NAME":"20GB/25GB","PRODUCT_AMOUNT":"7360"}, {"PRODUCT_CODE":"30000","PRODUCT_ID":"30000.01","PRODUCT_NAME":"42GB/52.5GB","PRODUCT_AMOUNT":"13800"}, {"PRODUCT_CODE":"45000","PRODUCT_ID":"45000.01","PRODUCT_NAME":"50GB/62.5GB","PRODUCT_AMOUNT":"16560"} ] } ], "9mobile": [ { "ID":"03","PRODUCT": [ {"PRODUCT_CODE":"500","PRODUCT_ID":"500.01","PRODUCT_NAME":"500MB","PRODUCT_AMOUNT":"475"}, {"PRODUCT_CODE":"1000","PRODUCT_ID":"1500.01","PRODUCT_NAME":"1.5GB","PRODUCT_AMOUNT":"950"}, {"PRODUCT_CODE":"1500","PRODUCT_ID":"2000.01","PRODUCT_NAME":"2GB","PRODUCT_AMOUNT":"1140"}, {"PRODUCT_CODE":"2500","PRODUCT_ID":"3000.01","PRODUCT_NAME":"3GB","PRODUCT_AMOUNT":"1425"}, {"PRODUCT_CODE":"4000","PRODUCT_ID":"4500.01","PRODUCT_NAME":"4.5GB","PRODUCT_AMOUNT":"1900"}, {"PRODUCT_CODE":"5500","PRODUCT_ID":"11000.01","PRODUCT_NAME":"11GB","PRODUCT_AMOUNT":"3800"}, {"PRODUCT_CODE":"11500","PRODUCT_ID":"15000.01","PRODUCT_NAME":"15GB","PRODUCT_AMOUNT":"4750"}, {"PRODUCT_CODE":"15000","PRODUCT_ID":"40000.01","PRODUCT_NAME":"40GB","PRODUCT_AMOUNT":"9500"}, {"PRODUCT_CODE":"27000","PRODUCT_ID":"75000.01","PRODUCT_NAME":"75GB","PRODUCT_AMOUNT":"14250"} ] } ], "Airtel": [ { "ID":"04","PRODUCT": [ {"PRODUCT_CODE":"1500","PRODUCT_ID":"1500.01","PRODUCT_NAME":"1.5GB","PRODUCT_AMOUNT":"950"}, {"PRODUCT_CODE":"3500","PRODUCT_ID":"3500.01","PRODUCT_NAME":"3.5GB","PRODUCT_AMOUNT":"1900"}, {"PRODUCT_CODE":"10000","PRODUCT_ID":"10000.01","PRODUCT_NAME":"10GB","PRODUCT_AMOUNT":"4750"}, {"PRODUCT_CODE":"16000","PRODUCT_ID":"16000.01","PRODUCT_NAME":"16GB","PRODUCT_AMOUNT":"7600"}, {"PRODUCT_CODE":"22000","PRODUCT_ID":"22000.01","PRODUCT_NAME":"22GB","PRODUCT_AMOUNT":"9500"} ] } ] } }';

    if (!empty($result = json_decode($json, 1))) {
        $MOBILE_NETWORK = $result['MOBILE_NETWORK'];
        var_dump($MOBILE_NETWORK);
    }

您可以从数组 $MOBILE_NETWORK 中获取的其他信息,例如:

foreach ($MOBILE_NETWORK['MTN'][0]["PRODUCT"][0] as $k => $product) {
    if ($k === 'PRODUCT_NAME') { echo '<option value="'.$product.'">'."\n"; }
}

foreach ($MOBILE_NETWORK['Glo'][0]["PRODUCT"][0] as $k => $product) {
    if ($k === 'PRODUCT_NAME') { echo '<option value="'.$product.'">'."\n"; }
}

foreach ($MOBILE_NETWORK['9mobile'][0]["PRODUCT"][0] as $k => $product) {
    if ($k === 'PRODUCT_NAME') { echo '<option value="'.$product.'">'."\n"; }
}

foreach ($MOBILE_NETWORK['Airtel'][0]["PRODUCT"][0] as $k => $product) {
    if ($k === 'PRODUCT_NAME') { echo '<option value="'.$product.'">'."\n"; }
}