Keys
You can add an element to an array without specifying a key.
/**
* PHP automaticaly assign a number as key ...
* The new key > the greatest key
*
* Array keys are case-sensitive, but type insensitive
*/
$A = array(2 => 5);
$B = array('4' => 5, 'a' => 'b');
$C = ['A' => 1, 'a' => 2, '1' => 3, 1=> 4, '01' => 5];
$A[] = 'a'; // key = 3
$B[] = 44; // key = 5
var_dump( $A[3] == 'a' ); // true
var_dump( $B[5] == 44 ); // true
var_dump( $C['A'] == $C['a'] ); // false
var_dump( $C['1'] == $C[1] ); // true
print_r($C); // A=>1, a=>2, 1=>4, 01=>5
List
List constructor assign array's values to individual variables.
/**
* list()
*
* Not really a function, but a language construct
* List doesn't work with strings, use explode()
*
* Extract assign multiple variables ...
* from key/value array
*/
$USER = ["John", "34", "07288333"];
$date = '25/05/2012';
$DATA = ['b'=>2, 'c'=>3];
list($name, $age) = $USER;
list($day, $month, $year) = explode('/', $date);
print_r("$name is $age years old"); // John is 34 years old
print_r($month); // 05
extract($DATA);
var_dump( $b == 2); // true
Last update: 360 days ago