<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Arrays on seth-shi</title><link>https://sethix.com/tags/arrays/</link><description>Recent content in Arrays on seth-shi</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Sun, 14 Mar 2021 12:07:42 +0800</lastBuildDate><atom:link href="https://sethix.com/tags/arrays/index.xml" rel="self" type="application/rss+xml"/><item><title>A Bug Caused by Combined Array Index Changes and Reference Parameters in PHP</title><link>https://sethix.com/posts/2021031412/</link><pubDate>Sun, 14 Mar 2021 12:07:42 +0800</pubDate><guid>https://sethix.com/posts/2021031412/</guid><description>Today, our team encountered a bug reported in the group chat. After cloning the project locally, I spent some time reproducing the issue.
Code Overview The following code causes the first item removal to convert a Collection into an object (JSON data): 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(); In the API data retrieval logic, this function call pattern caused issues: public function index() { // $json={&amp;#34;1&amp;#34;:{&amp;#34;id&amp;#34;: 2}} $data = Collection::make($json); // Filtering converts indexes to 0-based $index = $this-&amp;gt;getRandomAd($data, [1]); // Returns null due to original key mismatch 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>