$ cat array-destructuring-in-php.md
Array destructuring in PHP
<h2 id="list-or-[]"> <a href="https://www.stitcher.io/rss#list-or-%5B%5D" class="heading-anchor">#</a> List or []</h2> <p>In PHP, <code>list</code> or <code>[]</code> is a so called "language construct", just like <code>array()</code>. This language construct is used to "pull" variables out of an array. In other words: it will "destructure" the array into separate variables.</p> <p>Note that the word is "destructure", not "destruction" — that's something different 😉</p> <p>Here's what that looks like:</p> <pre><code class="language-php hljs php">$array = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]; <span class="hljs-comment">// Using the list syntax:</span> <span class="hljs-keyword">list</span>($a, $b, $c) = $array; <span class="hljs-comment">// Or the shorthand syntax:</span> [$a, $b, $c] = $array; <span class="hljs-comment">// $a = 1</span> <span class="hljs-comment">// $b = 2</span> <span class="hljs-comment">// $c = 3</span> </code></pre> <p>Whether you prefer <code>list</code> or its shorthand <code>[]</code> is up to you. People might argue that <code>[]</code> is ambiguous with the shorthand array syntax, and therefor prefer <code>list</code>. I'll be using the shorthand version in code samples though.</p> <p>So what more can <code>list</code> do?</p> <h2 id="skip-elements"> <a href="https://www.stitcher.io/rss#skip-elements" class="heading-anchor">#</a> Skip elements</h2> <p>Say you only need the third element of an array, the first two can be skipped by simply not providing a variable.</p> <pre><code class="language-php hljs php">[, , $c] = $array; <span class="hljs-comment">// $c = 3</span> </code></pre> <p>Also note that <code>list</code> will always start at index 0. Take for example the following array:</p> <pre><code class="language-php hljs php">$array = [ <span class="hljs-number">1</span> => <span class="hljs-string">'a'</span>, <span class="hljs-number">2</span> => <span class="hljs-string">'b'</span>, <span class="hljs-number">3</span> => <span class="hljs-string">'c'</span>, ]; </code></pre> <p>The first variable pulled out with <code>list</code> would be <code>null</code>, because there's no element with index <code>0</code>. This might seem like a shortcoming, but luckily there are more possibilities.</p> <h2 id="non-numerical-keys"> <a href="https://www.stitcher.io/rss#non-numerical-keys" class="heading-anchor">#</a> Non-numerical keys</h2> <p>PHP 7.1 allows <code>list</code> to be used with arrays that have non-numerical keys. This opens a world of possibilities.</p> <pre><code class="language-php hljs php">$array = [ <span class="hljs-string">'a'</span> => <span class="hljs-number">1</span>, <span class="hljs-string">'b'</span> => <span class="hljs-number">2</span>, <span class="hljs-string">'c'</span> => <span class="hljs-number">3</span>, ]; </code></pre> <pre><code class="language-php hljs php">[<span class="hljs-string">'c'</span> => $c, <span class="hljs-string">'a'</span> => $a] = $array; </code></pre> <p>As you can see, you can change the order however you want, and also skip elements entirely.</p> <h2 id="in-practice"> <a href="https://www.stitcher.io/rss#in-practice" class="heading-anchor">#</a> In practice</h2> <p>One of the uses of <code>list</code> are functions like <code>parse_url</code> and <code>pathinfo</code>. Because these functions return an array with named parameters, we can use <code>list</code> to pull out the information we'd like:</p> <pre><code class="language-php hljs php">[ <span class="hljs-string">'basename'</span> => $file, <span class="hljs-string">'dirname'</span> => $directory, ] = pathinfo(<span class="hljs-string">'/users/test/file.png'</span>); </code></pre> <p>As you can see, the variables don't need the same name as the key. Also note that destructuring an array with an unknown key will trigger a notice:</p> <pre><code class="language-php hljs php">[ <span class="hljs-string">'path'</span> => $path, <span class="hljs-string">'query'</span> => $query, ] = parse_url(<span class="hljs-string">'https://stitcher.io/blog'</span>); <span class="hljs-comment">// PHP Notice: Undefined index: query</span> </code></pre> <p>In this case, <code>$query</code> would be <code>null</code>.</p> <p>One last detail: trailing commas are allowed with named destructs, just like you're used to with arrays.</p> <h2 id="in-loops"> <a href="https://www.stitcher.io/rss#in-loops" class="heading-anchor">#</a> In loops</h2> <p>You can also use the list construct in loops:</p> <pre><code class="language-php hljs php">$array = [ [ <span class="hljs-string">'name'</span> => <span class="hljs-string">'a'</span>, <span class="hljs-string">'id'</span> => <span class="hljs-number">1</span> ], [ <span class="hljs-string">'name'</span> => <span class="hljs-string">'b'</span>, <span class="hljs-string">'id'</span> => <span class="hljs-number">2</span> ], ]; <span class="hljs-keyword">foreach</span> ($array <span class="hljs-keyword">as</span> [<span class="hljs-string">'id'</span> => $id, <span class="hljs-string">'name'</span> => $name]) { <span class="hljs-comment">// …</span> } </code></pre> <p>This could be useful when parsing for example a JSON or CSV file. Be careful though that undefined keys will still trigger a notice.</p> <p>In summary, there are some pretty good cases in which <code>list</code> can be of help!</p> <p></p> <blockquote class="sneaky-placeholder shown"> <p> This is where the ad would go. I get it, they are stupid. But to be honest, they actually make a difference. So maybe you'd consider whitelisting this blog or <a href="https://www.patreon.com/brendt" target="_blank" rel="noopener noreferrer">support me on Patreon</a>? </p> </blockquote> <div class="ad-container"> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-7163240497326235" data-ad-slot="4184710069"> </ins> </div>
source
This is a summary of an article first published by stitcher.io. The full piece is on their site.