mirror of
https://github.com/php/doc-pt_br.git
synced 2026-03-23 22:52:12 +01:00
299 lines
7.2 KiB
XML
299 lines
7.2 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: 8cdc6621f9826d04abc3e50438c010804d7e8683 Maintainer: lhsazevedo Status: ready --><!-- CREDITS: fabioluciano,lhsazevedo -->
|
|
<refentry xml:id="function.array-column" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>array_column</refname>
|
|
<refpurpose>Retorna os valores de uma coluna do array informado</refpurpose>
|
|
</refnamediv>
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type>array</type><methodname>array_column</methodname>
|
|
<methodparam><type>array</type><parameter>array</parameter></methodparam>
|
|
<methodparam><type class="union"><type>int</type><type>string</type><type>null</type></type><parameter>column_key</parameter></methodparam>
|
|
<methodparam choice="opt"><type class="union"><type>int</type><type>string</type><type>null</type></type><parameter>index_key</parameter><initializer>&null;</initializer></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
A função <function>array_column</function> retorna os valores de uma coluna do
|
|
<parameter>array</parameter> informado, indentificada pelo
|
|
<parameter>column_key</parameter>. Opcionalmente, uma
|
|
<parameter>index_key</parameter> pode ser informada para indexar os valores do
|
|
array retornado pelos valores da coluna <parameter>index_key</parameter>
|
|
do array.
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>array</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Um array multidimensional ou um array de objetos que se deseja extrair os
|
|
valores da coluna. Se um array de objetos for fornecido, propriedades
|
|
públicas podem ser extraídas diretamente. Para extrair propriedades
|
|
protegidas e privadas, a classe deve implementar ambos os métodos mágicos
|
|
<function>__get</function> e
|
|
<function>__isset</function>.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>column_key</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
A coluna de valores a ser retornada. Este valor pode ser uma chave inteira da
|
|
coluna que se deseja recuperar, ou uma uma string com o nome da chave de um
|
|
array associativo ou nome de propriedade. Também pode ser &null; para retornar
|
|
arrays completos ou objetos (isso é útil com o parâmetro
|
|
<parameter>index_key</parameter>, para reindexar o array).
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>index_key</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
A coluna a ser utilizada como índices/chaves do array retornado. Este valor
|
|
pode ser uma chave inteira da coluna, ou uma uma string com o nome da chave.
|
|
O valor é <link linkend="language.types.array.key-casts">convertido</link>
|
|
como de costume para chaves de array (no entanto, antes do PHP 8.0.0, objetos que suportam
|
|
conversão para string também eram permitidos).
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
Retorna um array com os valores que representam uma coluna do array informado.
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="changelog">
|
|
&reftitle.changelog;
|
|
<para>
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>8.0.0</entry>
|
|
<entry>
|
|
Objetos em colunas indicadas pelo parâmetro <parameter>index_key</parameter>
|
|
não serão mais convertidos em string e agora irão lançar um <classname>TypeError</classname> em vez disso.
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>Obtém a coluna first_name dos registros</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?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);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen role="php">
|
|
<![CDATA[
|
|
Array
|
|
(
|
|
[0] => John
|
|
[1] => Sally
|
|
[2] => Jane
|
|
[3] => Peter
|
|
)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>
|
|
Obtém a coluna last_name dos registros, indexados pela coluna "id"
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
// Using the $records array from Example #1
|
|
$last_names = array_column($records, 'last_name', 'id');
|
|
print_r($last_names);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen role="php">
|
|
<![CDATA[
|
|
Array
|
|
(
|
|
[2135] => Doe
|
|
[3245] => Smith
|
|
[5342] => Jones
|
|
[5623] => Doe
|
|
)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>
|
|
Obtém a coluna de nomes de usuários da propriedade pública "username" de um
|
|
objeto
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
class User
|
|
{
|
|
public $username;
|
|
|
|
public function __construct(string $username)
|
|
{
|
|
$this->username = $username;
|
|
}
|
|
}
|
|
|
|
$users = [
|
|
new User('user 1'),
|
|
new User('user 2'),
|
|
new User('user 3'),
|
|
];
|
|
|
|
print_r(array_column($users, 'username'));
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen role="php">
|
|
<![CDATA[
|
|
Array
|
|
(
|
|
[0] => user 1
|
|
[1] => user 2
|
|
[2] => user 3
|
|
)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>
|
|
Obtém a coluna de nomes da propriedade privada "name" de um objeto
|
|
utilizando o método mágico <function>__get</function>.
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
class Person
|
|
{
|
|
private $name;
|
|
|
|
public function __construct(string $name)
|
|
{
|
|
$this->name = $name;
|
|
}
|
|
|
|
public function __get($prop)
|
|
{
|
|
return $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'));
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen role="php">
|
|
<![CDATA[
|
|
Array
|
|
(
|
|
[0] => Fred
|
|
[1] => Jane
|
|
[2] => John
|
|
)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
Se o método <function>__isset</function> não for fornecido, um array vazio será
|
|
retornado.
|
|
</para>
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|