Skip to content

Instantly share code, notes, and snippets.

@jonataa
Last active August 29, 2015 14:01
Show Gist options
  • Save jonataa/ada3307081401a4b0dea to your computer and use it in GitHub Desktop.
Save jonataa/ada3307081401a4b0dea to your computer and use it in GitHub Desktop.
Trabalhando com Arrays no PHP
<?php
$lista = array(12 => 2, 5 => 1);
$lista[] = 50;
$lista['x'] = 200;
print_r($lista);
/**
Array
(
[12] => 2
[5] => 1
[13] => 50
[x] => 200
)
**/
<?php
$lista = array(5, 6, 7, 8, 9, 10);
$lista2 = [70, 80, 90, 100, 110]; // PHP >= 5.4
$lista3 = ['nome' => 'John Doe', 'idade' => 30];
$lista4 = [5 => 50, 6 => 60];
$lista5 = ['chave' => 'valor', false => 60]; // Apenas String e Integer como chave do array
<?php
$clientes[] = ['nome' => 'John Doe', 'idade' => 30];
$clientes[] = ['nome' => 'Foo Bar', 'idade' => 25];
$clientes[] = ['nome' => 'Jonata Weber', 'idade' => 44];
foreach ($clientes as $cliente) {
foreach ($cliente as $chave => $valor)
echo "$chave: $valor" . PHP_EOL;
}
/** Output:
nome: John Doe
idade: 30
nome: Foo Bar
idade: 25
nome: Jonata Weber
idade: 44
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment