Return the values from a single column in the input array (original) (raw)
Examples
Example #1 Get the column of first names from a recordset
<?php // Array representing a possible record set returned from a database $records = array( array( 'id' => 2135, 'first_name' => 'John', 'last_name' => 'Doe', ), array( 'id' => 3245, 'first_name' => 'Sally', 'last_name' => 'Smith', ), array( 'id' => 5342, 'first_name' => 'Jane', 'last_name' => 'Jones', ), array( 'id' => 5623, 'first_name' => 'Peter', 'last_name' => 'Doe', ) );$first_names = array_column($records, 'first_name'); print_r($first_names); ?>
The above example will output:
Array ( [0] => John [1] => Sally [2] => Jane [3] => Peter )
Example #2 Get the column of last names from a recordset, indexed by the "id" column
<?php // Using the $records array from Example #1 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi><mi>a</mi><mi>s</mi><msub><mi>t</mi><mi>n</mi></msub><mi>a</mi><mi>m</mi><mi>e</mi><mi>s</mi><mo>=</mo><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><msub><mi>y</mi><mi>c</mi></msub><mi>o</mi><mi>l</mi><mi>u</mi><mi>m</mi><mi>n</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">last_names = array_column(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8444em;vertical-align:-0.15em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">a</span><span class="mord mathnormal">s</span><span class="mord"><span class="mord mathnormal">t</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">n</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">am</span><span class="mord mathnormal">es</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:-0.0359em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">c</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">u</span><span class="mord mathnormal">mn</span><span class="mopen">(</span></span></span></span>records, 'last_name', 'id'); print_r($last_names); ?>
The above example will output:
Array ( [2135] => Doe [3245] => Smith [5342] => Jones [5623] => Doe )
Example #3 Get the column of usernames from the public "username" property of an object
`<?phpclass User
{
public $username;
public function
__construct(string $username)
{ this−>username=this->username = this−>username=username;
}
}$users = [
new User('user 1'),
new User('user 2'),
new User('user 3'),
];print_r(array_column($users, 'username'));
?> `
The above example will output:
Array ( [0] => user 1 [1] => user 2 [2] => user 3 )
Example #4 Get the column of names from the private "name" property of an object using the magic __get() method.
`<?phpclass Person
{
private $name;
public function
__construct(string $name)
{ this−>name=this->name = this−>name=name;
}
public function
__get($prop)
{
return this−>this->this−>prop;
}
public function
__isset($prop) : bool
{
return isset($this->$prop);
}
}$people = [
new Person('Fred'),
new Person('Jane'),
new Person('John'),
];print_r(array_column($people, 'name'));
?> `
The above example will output:
Array ( [0] => Fred [1] => Jane [2] => John )
If __isset() is not provided, then an empty array will be returned.