当键名称Laravel中有空格时,从数组访问数据

时间:2018-04-07 22:09:35

标签: php arrays json api laravel-5

我正在使用Alpha Vantage API并进行外部呼叫。问题是响应返回包含在字符串中的数值,并想知道我是否可以使用laravel 5.2解析它。我的控制器和视图与json结果一起在下面

    array:2 [▼
  "Meta Data" => array:4 [▼
    "1. Information" => "Monthly Adjusted Prices and Volumes"
    "2. Symbol" => "AAPL"
    "3. Last Refreshed" => "2018-04-06"
    "4. Time Zone" => "US/Eastern"
  ]
  "Monthly Adjusted Time Series" => array:219 [▼
    "2018-04-06" => array:7 [▼
      "1. open" => "167.8800"
      "2. high" => "174.2304"
      "3. low" => "164.4700"
      "4. close" => "168.3800"
      "5. adjusted close" => "168.3800"
      "6. volume" => "164408813"
      "7. dividend amount" => "0.0000"
    ]
    "2018-03-29" => array:7 [▼
      "1. open" => "178.5400"
      "2. high" => "183.5000"
      "3. low" => "164.9400"
      "4. close" => "167.7800"
      "5. adjusted close" => "167.7800"
      "6. volume" => "701387082"
      "7. dividend amount" => "0.0000"
    ]

视图,返回“尝试获取非对象属性”的错误我认为这是因为我试图声明的对象前面的数值和空格

@foreach($re as $Past)
    <tr>
    @foreach($Past as $key)
            <td>
            {{$key->open}}
            </td>
    @endforeach
    </tr>
@endforeach !>

控制器,我的控制器只是一个简单的API调用使用Guzzle

public function Test(){

        $client = new Client(); //GuzzleHttp\Client
        $promise = $client->get('https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY_ADJUSTED&symbol=AAPL&apikey=DEMOKey');
        $body = $promise->getbody();
        $result = json_decode($body, true);
         return view('pages.test', ['re' => $result]);
} 

1 个答案:

答案 0 :(得分:0)

您的问题的答案在于Eloquent: Serialization

在您的模型中添加此行

$hidden = ['field1', 'field2'];

这些字段现在从json回复中隐藏。