Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在foreach中使用空格访问JSON键_Php_Json_Foreach - Fatal编程技术网

Php 在foreach中使用空格访问JSON键

Php 在foreach中使用空格访问JSON键,php,json,foreach,Php,Json,Foreach,我正在尝试使用foreach访问下面的示例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"

我正在尝试使用foreach访问下面的示例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"
    }
}
问题是这些键有空格

我的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'],[Realtime Currency Exchange'],[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
}
知道我如何访问密钥及其值吗? 谢谢你可以这样做

$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>";
}

你不能用
,true)
将json_解码为数组吗?我试过像他的foreach($json->{'Realtime Currency Exchange'}一样作为$row){然后是$row_数组['From']=$row->{'1.From_Currency code'};但是结果是空的,我更新了我的答案。如果这对你有帮助,请接受我的答案。是的,谢谢
{
    "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"
    }]
}