Php 如何编写此代码,只对json进行一次解码?

Php 如何编写此代码,只对json进行一次解码?,php,json,Php,Json,如何编写此代码,只对json进行一次解码 $content = file_get_contents('http://www.xmlcharts.com/cache/precious-metals.php?format=json'); $json = ($content); foreach (json_decode($content, true) as $currency => $arr) { foreach ($arr

如何编写此代码,只对json进行一次解码

$content = file_get_contents('http://www.xmlcharts.com/cache/precious-metals.php?format=json'); 

          $json = ($content);

          foreach (json_decode($content, true) as $currency => $arr) { 
            foreach ($arr as $commodity => $price) { 
                if($currency == "usd" and $commodity =="gold")
              {
                $gold_price = round($price, 2);
              }
            } 
          }


          foreach (json_decode($content, true) as $currency => $arr) { 
            foreach ($arr as $commodity => $price) { 
                if($currency == "usd" and $commodity =="silver")
              {
                $silver_price = round($price, 2);
              }
            } 
          }


          foreach (json_decode($content, true) as $currency => $arr) { 
            foreach ($arr as $commodity => $price) { 
                if($currency == "usd" and $commodity =="platinum")
              {
                $platinum_price = round($price, 2);
              }
            } 
          }

将解码移动到一个变量

$decodedContent = json_decode($content, true);

foreach ($decodedContent as $currency => $arr) { 
            foreach ($arr as $commodity => $price) { 
                if($currency == "usd" and $commodity =="gold")
              {
                $gold_price = round($price, 2);
              }
            } 
          }
此外,您还应该通过在foreach中按$commodity进行切换来避免执行三重循环

 if($currency == "usd")
          {
            switch($commodity){
            case 'gold':
            $gold_price = round($price, 2);
            break;
            case 'silver':
            $silver_price = round($price, 2);
            break;
            case 'platinum':
            $platinum_price = round($price, 2);
            break;
            }
          }
那么:

foreach (json_decode($content, true) as $currency => $arr) { 
    foreach ($arr as $commodity => $price) { 
       if($currency == "usd") and )
       {
            if($commodity =="gold"){
                $gold_price = round($price, 2);
            } elseif($commodity =="platinum"){
                $platinum_price = round($price, 2);
            } elseif($commodity =="silver"){
                $silver_price = round($price, 2);
            }
        }
    } 
}

通过这种方式,您只需循环一次。

如果您将黄金、白银和铂金放入$target\u商品之类的数组中,这可能会更好,但这将使您只需循环一次JSON即可

$decoded_json = json_decode($content, true)

$commodity_prices = array(
    'gold' => 0,
    'silver' => 0,
    'platinum' => 0,
);

foreach ($decoded_json as $currency => $arr) { 
    foreach ($arr as $commodity => $price) {
        if ($currency == 'usd') {    
            switch ($commodity) {
            case 'gold':
            case 'silver':
            case 'platinum':
                $commodity_prices[$commodity] = round($price, 2);
                break;
            }
        }
    }
}

echo('Gold price: ' . $commodity_prices['gold']);
echo('Silver price: ' . $commodity_prices['silver']);
echo('Platinum price: ' . $commodity_prices['platinum']);

这里是一个简化版本的压缩代码,json_解码一次,没有额外的if/foreach循环

$content = file_get_contents('http://www.xmlcharts.com/cache/precious-metals.php?format=json'); 

// Decode contents    
$json = json_decode($content, true);

if(json_last_error() === JSON_ERROR_NONE) {

    // Define defaults for variables    
    $gold_price = '';
    $silver_price = '';
    $platinum_price = '';

    foreach ($json as $currency => $arr) { 
        foreach ($arr as $commodity => $price) { 
            if($currency == "usd") {
                switch($commodity) {
                    case "gold":
                        $gold_price = money_format('%i', $price);
                        break;
                    case "silver":
                        $silver_price = money_format('%i', $price);
                        break;
                    case "platinum":
                        $platinum_price = money_format('%i', $price);
                        break;
                }
            }
        } 
    }

} else {
    echo 'Invalid JSON';
    exit(0);
}

$json=json_decode$content,true;你为什么要循环三次呢?你可以用一个。你应该读一下:如果金银和铂金是唯一的商品,你可以扔掉开关盒。