<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.array.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.array-column.php',
    1 => 'array_column',
    2 => 'Return the values from a single column in the input array',
  ),
  'up' => 
  array (
    0 => 'ref.array.php',
    1 => 'Функції для роботи з масивами',
  ),
  'prev' => 
  array (
    0 => 'function.array-chunk.php',
    1 => 'array_chunk',
  ),
  'next' => 
  array (
    0 => 'function.array-combine.php',
    1 => 'array_combine',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/array/functions/array-column.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.array-column" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_column</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_column</span> &mdash; <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">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">function</span> <span class="methodname"><strong>array_column</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$array</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.null.php" class="type null">null</a></span></span> <code class="parameter">$column_key</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.null.php" class="type null">null</a></span></span> <code class="parameter">$index_key</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><a href="language.types.array.php" class="type array">array</a></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">array</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>
  <p class="simpara">
   <code class="parameter">array</code> is treated as a list of rows, each of which is
   itself an array or an object; a column is the entry or property that
   <code class="parameter">column_key</code> names in every row.
   Rows that are neither an array nor an object, and rows in which that entry
   or property is absent, are skipped without a diagnostic.
   The keys of <code class="parameter">array</code> are never preserved.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.array-column-parameters">
  <h3 class="title">Параметри</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">array</code></dt>
     <dd>
      <p class="simpara">
       A list whose rows are arrays or objects, from which a column of values
       is pulled. If a row is an object, public properties are read directly.
       For a property that is not publicly accessible to be read, the class
       must implement both the
       <a href="language.oop5.overloading.php#object.get" class="link">__get()</a> and
       <a href="language.oop5.overloading.php#object.isset" class="link">__isset()</a> magic methods: without
       <a href="language.oop5.overloading.php#object.isset" class="link">__isset()</a> the row is skipped, and
       with <a href="language.oop5.overloading.php#object.isset" class="link">__isset()</a> but no
       <a href="language.oop5.overloading.php#object.get" class="link">__get()</a> an
       <span class="exceptionname"><a href="class.error.php" class="exceptionname">Error</a></span> is thrown.
      </p>
     </dd>
    
    
     <dt><code class="parameter">column_key</code></dt>
     <dd>
      <p class="simpara">
       The column of values to return. This value may be an integer key of the
       column to retrieve, or it may be a string key name for an
       associative array or property name. It may also be <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> to return
       complete rows (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.
       The value is <a href="language.types.array.php#language.types.array.key-casts" class="link">cast</a>
       as usual for array keys (however, prior to PHP 8.0.0, objects supporting
       conversion to string were also allowed).
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.array-column-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="simpara">
   Returns an array of values representing a single column from the input
   array. If <code class="parameter">column_key</code> is <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>, the rows themselves
   are returned.
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.array-column-changelog">
  <h3 class="title">Журнал змін</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Версія</th>
       <th>Опис</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        Objects in columns indicated by <code class="parameter">index_key</code> parameter
        will no longer be cast to string and will now throw a <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> instead.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        Property visibility is now respected. Formerly, protected and private
        properties were read directly.
       </td>
      </tr>

      <tr>
       <td>7.0.0</td>
       <td>
        The rows of <code class="parameter">array</code> may now be objects as well as
        arrays.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.array-column-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Приклад #1 Get the column of first names from a recordset</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

</span><span style="color: #FF8000">// Array representing a possible record set returned from a database
</span><span style="color: #0000BB">$records </span><span style="color: #007700">= [
    [
        </span><span style="color: #DD0000">'id' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">2135</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'John'</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Doe'</span><span style="color: #007700">,
    ],
    [
        </span><span style="color: #DD0000">'id' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">3245</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Sally'</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Smith'</span><span style="color: #007700">,
    ],
    [
        </span><span style="color: #DD0000">'id' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">5342</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Jane'</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Jones'</span><span style="color: #007700">,
    ],
    [
        </span><span style="color: #DD0000">'id' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">5623</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Peter'</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Doe'</span><span style="color: #007700">,
    ]
];
 
</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">);
</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$first_names</span><span style="color: #007700">);

</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Array
(
    [0] =&gt; John
    [1] =&gt; Sally
    [2] =&gt; Jane
    [3] =&gt; Peter
)</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Приклад #2 
     Get the column of last names from a recordset, indexed by the &quot;id&quot; column
    </strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

</span><span style="color: #FF8000">// Using the $records array from Example #1
</span><span style="color: #0000BB">$records </span><span style="color: #007700">= [
    [
        </span><span style="color: #DD0000">'id' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">2135</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'John'</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Doe'</span><span style="color: #007700">,
    ],
    [
        </span><span style="color: #DD0000">'id' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">3245</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Sally'</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Smith'</span><span style="color: #007700">,
    ],
    [
        </span><span style="color: #DD0000">'id' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">5342</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Jane'</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Jones'</span><span style="color: #007700">,
    ],
    [
        </span><span style="color: #DD0000">'id' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">5623</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Peter'</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Doe'</span><span style="color: #007700">,
    ]
];

</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">);
</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$last_names</span><span style="color: #007700">);

</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Array
(
    [2135] =&gt; Doe
    [3245] =&gt; Smith
    [5342] =&gt; Jones
    [5623] =&gt; Doe
)</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Приклад #3 
     Get the column of usernames from the public &quot;username&quot; property of an
     object
    </strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class </span><span style="color: #0000BB">User
</span><span style="color: #007700">{
    public </span><span style="color: #0000BB">$username</span><span style="color: #007700">;

    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">)
    {
        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username </span><span style="color: #007700">= </span><span style="color: #0000BB">$username</span><span style="color: #007700">;
    }
}

</span><span style="color: #0000BB">$users </span><span style="color: #007700">= [
    new </span><span style="color: #0000BB">User</span><span style="color: #007700">(</span><span style="color: #DD0000">'user 1'</span><span style="color: #007700">),
    new </span><span style="color: #0000BB">User</span><span style="color: #007700">(</span><span style="color: #DD0000">'user 2'</span><span style="color: #007700">),
    new </span><span style="color: #0000BB">User</span><span style="color: #007700">(</span><span style="color: #DD0000">'user 3'</span><span style="color: #007700">),
];

</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">));

</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Array
(
    [0] =&gt; user 1
    [1] =&gt; user 2
    [2] =&gt; user 3
)</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4">
    <p><strong>Приклад #4 
     Get the column of names from the private &quot;name&quot; property of an object
     using the magic methods <a href="language.oop5.overloading.php#object.isset" class="link">__isset()</a> and <a href="language.oop5.overloading.php#object.get" class="link">__get()</a>
    </strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class </span><span style="color: #0000BB">Person
</span><span style="color: #007700">{
    private </span><span style="color: #0000BB">$name</span><span style="color: #007700">;

    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">)
    {
        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #0000BB">$name</span><span style="color: #007700">;
    }

    public function </span><span style="color: #0000BB">__get</span><span style="color: #007700">(</span><span style="color: #0000BB">$prop</span><span style="color: #007700">)
    {
        return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$prop</span><span style="color: #007700">;
    }

    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
    </span><span style="color: #007700">{
        return isset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$prop</span><span style="color: #007700">);
    }
}

</span><span style="color: #0000BB">$people </span><span style="color: #007700">= [
    new </span><span style="color: #0000BB">Person</span><span style="color: #007700">(</span><span style="color: #DD0000">'Fred'</span><span style="color: #007700">),
    new </span><span style="color: #0000BB">Person</span><span style="color: #007700">(</span><span style="color: #DD0000">'Jane'</span><span style="color: #007700">),
    new </span><span style="color: #0000BB">Person</span><span style="color: #007700">(</span><span style="color: #DD0000">'John'</span><span style="color: #007700">),
];

</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">));
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Array
(
    [0] =&gt; Fred
    [1] =&gt; Jane
    [2] =&gt; John
)</pre>
</div>
    </div>
   </div>
   If <a href="language.oop5.overloading.php#object.isset" class="link">__isset()</a> is not provided, the object
   contributes no entry to the returned array.
  </p>
 </div>


</div><?php manual_footer($setup); ?>