<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Return the values from a single column in the input array</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-chunk.html">array_chunk</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-combine.html">array_combine</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.array-column" class="refentry"> <div class="refnamediv"> <h1 class="refname">array_column</h1> <p class="verinfo">(PHP 5 >= 5.5.0, PHP 7)</p><p class="refpurpose"><span class="refname">array_column</span> — <span class="dc-title">Return the values from a single column in the input array</span></p> </div> <div class="refsect1 description" id="refsect1-function.array-column-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>array_column</strong></span> ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$input</code></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$column_key</code></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$index_key</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>array_column()</strong></span> returns the values from a single column of the <code class="parameter">input</code>, identified by the <code class="parameter">column_key</code>. Optionally, an <code class="parameter">index_key</code> may be provided to index the values in the returned array by the values from the <code class="parameter">index_key</code> column of the input array. </p> </div> <div class="refsect1 parameters" id="refsect1-function.array-column-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">input</code></dt> <dd> <p class="para"> A multi-dimensional array or an array of objects from which to pull a column of values from. If an array of objects is provided, then public properties can be directly pulled. In order for protected or private properties to be pulled, the class must implement both the <span class="function"><strong>__get()</strong></span> and <span class="function"><strong>__isset()</strong></span> magic methods. </p> </dd> <dt> <code class="parameter">column_key</code></dt> <dd> <p class="para"> The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. It may also be <strong><code>NULL</code></strong> to return complete arrays or objects (this is useful together with <code class="parameter">index_key</code> to reindex the array). </p> </dd> <dt> <code class="parameter">index_key</code></dt> <dd> <p class="para"> The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.array-column-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an array of values representing a single column from the input array. </p> </div> <div class="refsect1 changelog" id="refsect1-function.array-column-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>7.0.0</td> <td> Added the ability for the <code class="parameter">input</code> parameter to be an array of objects. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.array-column-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-6143"> <p><strong>Example #1 Get the column of first names from a recordset</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Array representing a possible record set returned from a database<br /></span><span style="color: #0000BB">$records </span><span style="color: #007700">= array(<br /> array(<br /> </span><span style="color: #DD0000">'id' </span><span style="color: #007700">=> </span><span style="color: #0000BB">2135</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'John'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Doe'</span><span style="color: #007700">,<br /> ),<br /> array(<br /> </span><span style="color: #DD0000">'id' </span><span style="color: #007700">=> </span><span style="color: #0000BB">3245</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Sally'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Smith'</span><span style="color: #007700">,<br /> ),<br /> array(<br /> </span><span style="color: #DD0000">'id' </span><span style="color: #007700">=> </span><span style="color: #0000BB">5342</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Jane'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Jones'</span><span style="color: #007700">,<br /> ),<br /> array(<br /> </span><span style="color: #DD0000">'id' </span><span style="color: #007700">=> </span><span style="color: #0000BB">5623</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Peter'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Doe'</span><span style="color: #007700">,<br /> )<br />);<br /> <br /></span><span style="color: #0000BB">$first_names </span><span style="color: #007700">= </span><span style="color: #0000BB">array_column</span><span style="color: #007700">(</span><span style="color: #0000BB">$records</span><span style="color: #007700">, </span><span style="color: #DD0000">'first_name'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$first_names</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Array ( [0] => John [1] => Sally [2] => Jane [3] => Peter ) </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-6144"> <p><strong>Example #2 Get the column of last names from a recordset, indexed by the "id" column </strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Using the $records array from Example #1<br /></span><span style="color: #0000BB">$last_names </span><span style="color: #007700">= </span><span style="color: #0000BB">array_column</span><span style="color: #007700">(</span><span style="color: #0000BB">$records</span><span style="color: #007700">, </span><span style="color: #DD0000">'last_name'</span><span style="color: #007700">, </span><span style="color: #DD0000">'id'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$last_names</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Array ( [2135] => Doe [3245] => Smith [5342] => Jones [5623] => Doe ) </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-6145"> <p><strong>Example #3 Get the column of usernames from the public "username" property of an object </strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">User<br /></span><span style="color: #007700">{<br /> public </span><span style="color: #0000BB">$username</span><span style="color: #007700">;<br /><br /> public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">string $username</span><span style="color: #007700">)<br /> {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">username </span><span style="color: #007700">= </span><span style="color: #0000BB">$username</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$users </span><span style="color: #007700">= [<br /> new </span><span style="color: #0000BB">User</span><span style="color: #007700">(</span><span style="color: #DD0000">'user 1'</span><span style="color: #007700">),<br /> new </span><span style="color: #0000BB">User</span><span style="color: #007700">(</span><span style="color: #DD0000">'user 2'</span><span style="color: #007700">),<br /> new </span><span style="color: #0000BB">User</span><span style="color: #007700">(</span><span style="color: #DD0000">'user 3'</span><span style="color: #007700">),<br />];<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_column</span><span style="color: #007700">(</span><span style="color: #0000BB">$users</span><span style="color: #007700">, </span><span style="color: #DD0000">'username'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Array ( [0] => user 1 [1] => user 2 [2] => user 3 ) </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-6146"> <p><strong>Example #4 Get the column of names from the private "name" property of an object using the magic <span class="function"><strong>__get()</strong></span> method. </strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Person<br /></span><span style="color: #007700">{<br /> private </span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br /><br /> public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">string $name</span><span style="color: #007700">)<br /> {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br /> }<br /><br /> public function </span><span style="color: #0000BB">__get</span><span style="color: #007700">(</span><span style="color: #0000BB">$prop</span><span style="color: #007700">)<br /> {<br /> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">$prop</span><span style="color: #007700">;<br /> }<br /><br /> public function </span><span style="color: #0000BB">__isset</span><span style="color: #007700">(</span><span style="color: #0000BB">$prop</span><span style="color: #007700">) : </span><span style="color: #0000BB">bool<br /> </span><span style="color: #007700">{<br /> return isset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">$prop</span><span style="color: #007700">);<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$people </span><span style="color: #007700">= [<br /> new </span><span style="color: #0000BB">Person</span><span style="color: #007700">(</span><span style="color: #DD0000">'Fred'</span><span style="color: #007700">),<br /> new </span><span style="color: #0000BB">Person</span><span style="color: #007700">(</span><span style="color: #DD0000">'Jane'</span><span style="color: #007700">),<br /> new </span><span style="color: #0000BB">Person</span><span style="color: #007700">(</span><span style="color: #DD0000">'John'</span><span style="color: #007700">),<br />];<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_column</span><span style="color: #007700">(</span><span style="color: #0000BB">$people</span><span style="color: #007700">, </span><span style="color: #DD0000">'name'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Array ( [0] => Fred [1] => Jane [2] => John ) </pre></div> </div> </div> If <span class="function"><strong>__isset()</strong></span> is not provided, then an empty array will be returned. </p> </div> <div class="refsect1 seealso" id="refsect1-function.array-column-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <a href="https://github.com/ramsey/array_column/blob/master/src/array_column.php" class="link external">» Recommended userland implementation for PHP lower than 5.5</a> </li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-chunk.html">array_chunk</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-combine.html">array_combine</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>