<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>数组 on 施国鹏 (seth-shi)</title><link>https://sethix.com/zh-cn/tags/%E6%95%B0%E7%BB%84/</link><description>Recent content in 数组 on 施国鹏 (seth-shi)</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Sun, 14 Mar 2021 12:07:42 +0800</lastBuildDate><atom:link href="https://sethix.com/zh-cn/tags/%E6%95%B0%E7%BB%84/index.xml" rel="self" type="application/rss+xml"/><item><title>对于数组索引变化+引用参数共同导致出现的一个 bug</title><link>https://sethix.com/zh-cn/posts/2021031412/</link><pubDate>Sun, 14 Mar 2021 12:07:42 +0800</pubDate><guid>https://sethix.com/zh-cn/posts/2021031412/</guid><description>今天, 我们老板在群里发出了一个bug, 然后我打开了项目, 进行本地复现. 过了一段时间. 终于复现了这个问题
大致代码如下 以下代码会导致移除第一项的时候, 把一个Collection数组转化成对象(JSON数据) protected function transformJson($items) { return Collection::make($items) - -&amp;gt;values() -&amp;gt;where(&amp;#39;_remove_&amp;#39;, &amp;#39;0&amp;#39;) + -&amp;gt;values() -&amp;gt;map(fn($item) =&amp;gt; Arr::except($item, &amp;#39;_remove_&amp;#39;)) -&amp;gt;toJson(); 在API获取数据的时候, 由于有这样的一段代码. 并且是函数的调用 public function index() { // $json={&amp;#34;1&amp;#34;:{&amp;#34;id&amp;#34;: 2}} $data = Collection::make($json); // 由于进行了 filter, 所以得到了 0 的 index $index = $this-&amp;gt;getRandomAd($data, [1]); // 因为 Collection 中只有 1 的 key, 所以返回了 null return $data-&amp;gt;get($index); } protected function getRandomAd(Collection $data, $notIn=[]) { $index = -1; if (count($notIn) &amp;gt; 0) { $data = $data-&amp;gt;filter(fn($item) =&amp;gt; !</description></item></channel></rss>