Php - Array (Map)

1 - About

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

Array can contain others arrays.

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

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


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[0]); 

  • the whole array

unset($myArray); 

  • a value

if (($key = array_search($value, $array)) !== false) {
    unset($array[$key]);
}

4.5 - (List|Print)

  • To the console

var_dump($array);
print_r($array); // Echo Human readable information

  • In a variable (Retrieve the information in a variable, ie Return the output instead of printing it)

arrayInfo = print_r($array,true); 

  • Json

$json = json_encode($_REQUEST)

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

  • Php - foreach
  • PHP - For loop
  • PHP - While

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.8.3 - Functional Programming

See PHP - Functional Programming

4.9 - Count


count($array);

4.10 - Replace

4.11 - Insert

array_splice


array_splice( $original, 3, 0, $inserted );

insert into $origin at index 3, deleting 0, inserting the array $inserted

4.12 - Compare

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

4.13 - Sorting

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


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

4.14 - 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.15 - Join (to String)


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

4.16 - Sort

4.16.1 - Sort by value

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'];
});

4.16.2 - Sort by key

  • ksort - Sort an array by key
  • asort - Sort an array and maintain index association

4.17 - Merge / Union

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


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap