In Php, an array is an (ordered|sorted) map (keys/value)
Array can contain others arrays.
The key can either be an integer or a string (in the same array - PHP does not distinguish between indexed and associative arrays. ).
While automatically indexing occurs, php will start with the integer 0 and add an increment of 1.
'bar' is a string, while bar is an undefined constant. PHP automatically converts a bar string (an unquoted string which does not correspond to any known symbol) into a string which contains the bar string.
Beware that for other data type, the key will be casted Ie a float of 1.5 will be casted to 1).
The value can be of any type.
Because the value of an array can be anything, it can also be another array. This enables the creation of recursive and multi-dimensional arrays.
// Create a new multi-dimensional array
$juices["apple"]["green"] = "good";
sizeof($array)
offset is not a property but is used by function such as array_splice
To retrieve the offset from a key, search the array of key
$offset = array_search($key, array_keys($myArray), true);
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
// Before 5.4
$myArray = array();
// As of PHP 5.4
$myArray = []; -- empty array, inside the square brackets, you have an expression. $arr[somefunc($bar)] will work
$array = [
"key" => "value",
"foo" => "bar",
"bar" => "foo",
];
//or
$array = array (
"key" => "value",
"foo" => "bar",
"bar" => "foo",
);
Array assignment always involves value copying. (ie not by reference).
array_unshift($array, "first");
array_unshift($queue, "0", "1");
$input[] = $x; // assign the next index automatically (ie the last one + 1)
array_push($input, $x, $y);
array_splice($input, count($input), 0, array($x, $y));
// insert into ''$origin'' at index 3, deleting 0, inserting the array ''$inserted''
array_splice( $original, 3, 0, $inserted );
$result = array_merge($array1, $array2);
if array_key_exists('key', $myarray) {
echo "The 'key' element is in the array";
}
if in_array('value', $myarray) {
echo "'value' is in the array";
}
if in_array($object, $myarray, TRUE) {
echo "'object' is in the array";
}
unset($myArray[0]);
$myArray[0] == null
array_splice ( $array , 0 , 1 , [] );
$myArray[0] == "value of second element"
if (($key = array_search($valueToDelete, $array)) !== false) {
unset($array[$key]);
}
unset($myArray);
if (($key = array_search($value, $array)) !== false) {
unset($array[$key]);
}
$lastElement = array_pop($array);
$lastElement = array_shift($array);
var_dump($array);
print_r($array); // Echo Human readable information
arrayInfo = print_r($array,true);
$json = json_encode($_REQUEST)
Reindexed using the array_values() function
array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] ) : array
See list
// My
list($var1, $var2, $var3, $var4) = $myArrayOf4Variables;
// From the doc
$info = array('coffee', 'brown', 'caffeine');
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";
See PHP - Functional Programming
count($array);
$myArray[1] = "one";
$myArray[3] = "three";
It is possible to compare arrays with the array_diff() function and with array operators.
Arrays are ordered. The order can be changed using various sorting functions.
<?php
sort($files);
print_r($files);
?>
Array assignment always involves value copying. Use the reference operator to copy an array by reference.
$a =& $b; -- =& is the reference operator
a and b point to the same variable space.
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
Ordinal Data - Sorting (Problem / Algorithm) an array in php in ascendant
// if the array contains number
usort($myArray, function($a, $b) {
return $b - $a; // $a - $b descendant
});
// if the value contains array and that you want to sort on a key
usort($myArray, function($a, $b) {
return $b['key'] - $a['key'];
});
if the arrays contain numeric keys, the value will not overwrite the original value, but will be appended.
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);