在foreach中使用空格访问JSON密钥

时间:2018-08-14 05:27:28

标签: php json foreach

我正在尝试使用以下示例JSON 中的foreach每个键值进行访问。

{
  "Realtime Currency Exchange Rate": 
    {
        "1. From_Currency Code": "EUR",
        "2. From_Currency Name": "Euro",
        "3. To_Currency Code": "USD",
        "4. To_Currency Name": "United States Dollar",
        "5. Exchange Rate": "1.14122552",
        "6. Last Refreshed": "2018-08-14 05:07:51",
        "7. Time Zone": "UTC"
    },
    {
        "1. From_Currency Code": "USD",
        "2. From_Currency Name": "United States Dollar",
        "3. To_Currency Code": "EUR",
        "4. To_Currency Name": "Euro",
        "5. Exchange Rate": "0.87692400",
        "6. Last Refreshed": "2018-08-14 05:09:17",
        "7. Time Zone": "UTC"
    }
}

问题是按键有空格。

我的 FOREACH 看起来像这样

$json = json_decode($data);    
$json_response = array();
foreach ($json->Realtime Currency Exchange Rate as $row) {
        $row_array = array();     
        $row_array['From'] = $row->1. From_Currency Code;        
        $row_array['To'] = $row->3. To_Currency Code;
        $row_array['value'] = $row->5. Exchange Rate;                           
       array_push($json_response, $row_array); 
 }

我试图将foreach语句中的密钥用作['Realtime Currency Exchange Rate'], [Realtime Currency Exchange Rate], [Realtime.Currency.Exchange.Rate]以及我在这里可以找到的其他任何内容,但没有一个起作用。

编辑 也尝试过这种方法,但是没有用

foreach ($json->{'Realtime Currency Exchange Rate'} as $row) {
    $row_array = array();

        $row_array['From'] = $row->{'1. From_Currency Code'};

   array_push($json_response, $row_array); //push the values in the array
}

但是这个解决方案有效

foreach ($json as $row) {
    $row_array = array();

        $row_array['From'] = $row->{'1. From_Currency Code'};

   array_push($json_response, $row_array); //push the values in the array
}

有什么办法可以访问密钥及其值吗? 谢谢

1 个答案:

答案 0 :(得分:2)

您可以这样做

$json->{'Realtime Currency Exchange Rate'}

希望有帮助

更新:

$data = array(
    "Realtime Currency Exchange Rate" => array(
        0 => array(
            "1. From_Currency Code" => "EUR",
            "2. From_Currency Name" => "Euro",
            "3. To_Currency Code" => "USD",
            "4. To_Currency Name"=> "United States Dollar",
            "5. Exchange Rate" => "1.14122552",
            "6. Last Refreshed" => "2018-08-14 05:07:51",
            "7. Time Zone" => "UTC"
        ),
        1 => array(
            "1. From_Currency Code" => "USD",
            "2. From_Currency Name" => "United States Dollar",
            "3. To_Currency Code" => "EUR",
            "4. To_Currency Name" => "Euro",
            "5. Exchange Rate" => "0.87692400",
            "6. Last Refreshed" => "2018-08-14 05:09:17",
            "7. Time Zone" => "UTC"
        )
    )
);

$data = json_encode($data);

$json = json_decode($data);    
$json_response = array();
foreach ($json->{'Realtime Currency Exchange Rate'} as $row) {
    echo "<pre>";
    print_r($row);
    echo "</pre>";
}

格式化的JSON字符串如下所示

{
    "Realtime Currency Exchange Rate": [{
        "1. From_Currency Code": "EUR",
        "2. From_Currency Name": "Euro",
        "3. To_Currency Code": "USD",
        "4. To_Currency Name": "United States Dollar",
        "5. Exchange Rate": "1.14122552",
        "6. Last Refreshed": "2018-08-14 05:07:51",
        "7. Time Zone": "UTC"
    }, {
        "1. From_Currency Code": "USD",
        "2. From_Currency Name": "United States Dollar",
        "3. To_Currency Code": "EUR",
        "4. To_Currency Name": "Euro",
        "5. Exchange Rate": "0.87692400",
        "6. Last Refreshed": "2018-08-14 05:09:17",
        "7. Time Zone": "UTC"
    }]
}