2017-05-25 31 views
1

我有這樣一段代碼:問題與Laravel 5.4,其中收集方法

if ($response && $response->getStatusCode() == 200) { 
     $this->ticker = collect(collect(json_decode($response->getBody()->getContents(), true))->get('payload')); 

     Log::info(json_encode($this->ticker)); 

     switch ($book): 
      case 'btc_mxn': 
       return $this->ticker->whereStrict('book', 'btc_mxn'); 
      case 'eth_mxn': 
       return $this->ticker->whereStrict('book', 'eth_mxn'); 
      case 'all':  
       return $this->ticker; 
      default: 
       throw new ValidationHttpException(['Invalid book (9002)']); 
     endswitch; 
    } 

的$響應部分並不重要,只有$這個 - >股票部分。這會得到一個包含對象數組的API響應。 對象包含我關心的書籍字段。將JSON看起來是這樣的:

[{"high":"59999.00","last":"53021.11","created_at":"2017-05-25T23:16:44+00:00","book":"btc_mxn","volume":"1313.28966742","vwap":"55354.76622471","low":"50000.00","ask":"53998.92","bid":"53021.11"},{"high":"4699.00","last":"4102.00","created_at":"2017-05-25T23:16:44+00:00","book":"eth_mxn","volume":"7162.16835199","vwap":"4322.60134630","low":"3900.00","ask":"4102.00","bid":"4100.00"},{"high":"0.00012498","last":"0.00010700","created_at":"2017-05-25T23:16:44+00:00","book":"xrp_btc","volume":"17584.07258163","vwap":"0.00010897","low":"0.00009500","ask":"0.00011990","bid":"0.00010100"},{"high":"7.10","last":"6.05","created_at":"2017-05-25T23:16:44+00:00","book":"xrp_mxn","volume":"1015137.88406527","vwap":"6.28004670","low":"5.50","ask":"6.05","bid":"5.85"},{"high":"0.08197000","last":"0.07800000","created_at":"2017-05-25T23:16:44+00:00","book":"eth_btc","volume":"73.29999906","vwap":"0.07656212","low":"0.07250000","ask":"0.07800000","bid":"0.07600000"}] 

問題是當我切換$書。如果$書= 'btc_mxn' 我得到的對象數組:

[ 
    { 
    "high": "59999.00", 
    "last": "53000.00", 
    "created_at": "2017-05-25T23:23:29+00:00", 
    "book": "btc_mxn", 
    "volume": "1316.43950673", 
    "vwap": "55323.60047189", 
    "low": "50000.00", 
    "ask": "53000.00", 
    "bid": "52001.00" 
    } 
] 

但是,如果$書= 'eth_mxn' 我得到一個對象!

{ 
    "1": { 
    "high": "4699.00", 
    "last": "4025.97", 
    "created_at": "2017-05-25T23:24:18+00:00", 
    "book": "eth_mxn", 
    "volume": "7360.11920724", 
    "vwap": "4310.48584845", 
    "low": "3900.00", 
    "ask": "4026.00", 
    "bid": "4000.01" 
    } 
} 

同樣的情況幾乎到這是不是「btc_mxn」其他書的關鍵。

我通過向first()方法添加一個調用來解決這個問題,但這很奇怪。有人對這裏發生的事情有任何想法嗎?

謝謝。

+0

這個問題/答案是有關:https://stackoverflow.com/questions/36231077/laravel-eloquent-toarray-not-using-square-braces/ 36232302#36232302 – patricus

回答

1

書「btc_mxn」是第一本在集合中,與0的指數所以,你where結果看起來類似:

[ 
    0 => { /* book data */ } 
] 

在JSON,這是一個正確的數字索引數組,因此它被表示爲一個元素的數組。

但書「eth_mxn」是第二本書您的收藏中,以1爲指數所以,你where結果看起來類似:

[ 
    1 => { /* book data */ } 
] 

在JSON,這不是一個正確的數字索引數組,因爲索引數組必須具有從索引0開始的連續鍵。由於這不是有效的json數組,因此它被表示爲數字鍵屬於對象的對象。

如果您希望將其作爲數組表示,您需要重新鍵入where()調用的結果集合。您可以使用values()方法重新鍵入結果:

case 'btc_mxn': 
    return $this->ticker->whereStrict('book', 'btc_mxn')->values(); 
case 'eth_mxn': 
    return $this->ticker->whereStrict('book', 'eth_mxn')->values(); 
+0

非常有意義。非常感謝你。 – Jonathan