【问题标题】:Issue with Laravel 5.4 where collection methodLaravel 5.4 的问题收集方法
【发布时间】:2017-10-26 17:45:41
【问题描述】:

我有这段代码:

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

$response 部分并不重要,只有 $this->ticker 部分。这将获得一个包含对象数组的 API 响应。 对象包含我关心的 book 字段。 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"}] 

当我切换 $book 时,问题就来了。如果 $book = '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"
  }
]

但是,如果 $book = '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() 方法的调用来解决此问题,但这很奇怪。有人知道这里发生了什么吗?

谢谢。

【问题讨论】:

标签: php laravel laravel-5 collections


【解决方案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();

【讨论】:

  • 非常有意义。非常感谢。
猜你喜欢
  • 2017-10-21
  • 1970-01-01
  • 2018-04-25
  • 1970-01-01
  • 2017-07-14
  • 1970-01-01
  • 2011-07-25
  • 2018-05-14
  • 2017-07-21
相关资源
最近更新 更多