Last active
August 29, 2015 14:01
-
-
Save jonataa/ada3307081401a4b0dea to your computer and use it in GitHub Desktop.
Trabalhando com Arrays no PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$lista = array(12 => 2, 5 => 1); | |
$lista[] = 50; | |
$lista['x'] = 200; | |
print_r($lista); | |
/** | |
Array | |
( | |
[12] => 2 | |
[5] => 1 | |
[13] => 50 | |
[x] => 200 | |
) | |
**/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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