Php - Array

> Procedural Languages > Php

1 - About

In Php, an array is an (ordered|sorted) map (keys/value)

Array can contain others arrays.

Advertising

3 - Element Structure

3.1 - (Key|Index)

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).

3.2 - Value

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"; 

3.3 - Length

sizeof($array)

4 - Management

4.1 - Initialization

If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.

4.1.1 - Empty

// 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

4.1.2 - Non Empty

$array = [
    "key" => "value",
    "foo" => "bar",
    "bar" => "foo",
];
//or
$array = array (
    "key" => "value",
    "foo" => "bar",
    "bar" => "foo",
);

4.2 - Assignment

Array assignment always involves value copying. (ie not by reference).

$myArray[] = "null";  // assign the next index automatically (ie 0)
$myArray[1] = "one";
$myArray[] = "two"; // assign the next index automatically (ie the last one + 1)
$myArray[3] = "three";
Advertising

4.2.1 - At the beginning

array_unshift($array, "first", "second");

4.2.2 - At the end

$myArray[] = "end"; // assign the next index automatically (ie the last one + 1)

4.3 - Key / Value Existence (In Operator)

if array_key_exists('key', $myarray) {
    echo "The 'key' element is in the array";
}
  • in_array — Checks if a value exists in an array
if in_array('value', $myarray) {
    echo "'value' is in the array";
}

4.4 - Remove

  • the first element
unset($myArray[1]); 
  • the whole array
unset($myArray); 
Advertising

4.5 - (List|Print)

var_dump($array);
print_r($array); // Echo Human readable information
print_r($array, true); // Return the output instead of printing it
arrayInfo = print_r($array,true); // Retrieve the information in a variable

4.6 - Reindex

Reindexed using the array_values() function

4.7 - Slice

array_slice

array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] ) : array

4.8 - Loop

4.8.1 - For Each

4.8.2 - List

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";

4.9 - Count

count($array);

4.10 - Compare

It is possible to compare arrays with the array_diff() function and with array operators.

4.11 - Sorting

Arrays are ordered. The order can be changed using various sorting functions.

<?php
sort($files);
print_r($files);
?>

4.12 - Copy

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.

4.13 - Conversion

4.13.1 - To String

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone

4.14 - Sort

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 array contains array and that you want to sort on the key
usort($myArray, function($a, $b) {
    return $b['key'] - $a['key'];
});

4.15 - Merge

array-merge

$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);

5 - Documentation / Reference