Forside > PHP > PHP Arrays

PHP: Arrays

By Kristoffer Bohmann

What Arrays are, and what they can do for you.

See also: Array function reference.

  • Array is a data type.
  • Array data is organized in a list-like format. Example: $arr = Array( "first_name"=>"Kristoffer", "last_name"=>"Bohmann").
  • Array data can associate an index key with a value - making it easy to lookup the value (based on the index key). Example: $arr[first_name] holds my first name ("Kristoffer").
    Array data is stored in a single variable.
  • Array keys can be Numerical, Associative or both.
  • Array data can be stored with serialize().

Numerical Arrays

abc

Associative Arrays

abc

Array Types: Numerical, Associative and Multidimensional

abc

Array Operators

Operator Example Explanation
+ $a + $b Union of $a and $b.
== $a == $b TRUE if $a and $b have same key/value pairs.
=== $a === $b TRUE if $a and $b have same key/value pairs, in the same order, and of the same types.
!= $a != $b TRUE if $a is not equal to $b.
<> $a <> $b TRUE if $a is not equal to $b (same as above).
!== $a !== $b TRUE if $a is not identical to $b (key/value pairs differ).

Looping an Array with foreach, for and while

Numerical, Associative and Multidimensional

Get Array data

abc

Set and Update Array data

abc

Setting Array data manually

$fruits[] = "Apple";

$fruits[] = "Pear";

$fruits[] = "Banana";

Outputs: Array ( [0] => Apple [1] => Pear [2] => Banana )

Same as:

$fruits = Array("Apple", "Pear", "Banana");

Delete (Unlink) Array data

Delete a value.

Delete a key.

Delete an array.

Sort Array data

abc