PHP – Array Defined

0 0
Read Time:3 Minute, 18 Second

PHP – Array Defined

  • The array() function is used to create an array.
  • An Array is a structure which maps one value to another.
    • ex: we can map the integer value 1 to the string “Monday”
    • The value 1 in this case is called a “key”
    • The string “Monday” is called the key’s “value”
  • An array is made up of a group of these key/value pairs
  • An array is arranged so that each item in the array can be located by using the key when needed
  • Array Types are:
    • Associative (TIP: look for => it denotes the key value)
    • Numeric or Indexed (Integers keys are automatically or manually assigned, keys automatically start with 0, a common source of error)
    • Multidimensional (one or more values in the array is another array)

Note: A short array syntax exists which replaces array() with [ ].

Example #1 A simple array

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// Using the short array syntax
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
?>

The key can either be an int or a string. The value can be of any type.

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

Example #2 Type Casting and Overwriting example

<?php

   $array = array(
    1    => "a",
    "1"  => "b",
    1.5  => "c",
    true => "d",
);
var_dump($array);

?>

The above example will output:

array(1) {
  [1]=>
  string(1) "d"
}


As all the keys in the above example are cast to 1, the value will be overwritten on every new element and the last assigned value “d” is the only one left over.

PHP arrays can contain int and string keys at the same time as PHP does not distinguish between indexed and associative arrays.

Example #3 Mixed int and string keys

<?php
  $array = array(
    "foo" => "bar",
    "bar" => "foo",
    100   => -100,
    -100  => 100,
);
var_dump($array);
?>

The above example will output:

array(4) {
  ["foo"]=>
  string(3) "bar"
  ["bar"]=>
  string(3) "foo"
  [100]=>
  int(-100)
  [-100]=>
  int(100)
}

The key is optional. If it is not specified, PHP will use the increment of the largest previously used int key.

<?php
$array = array("foo", "bar", "hello", "world");
var_dump($array);
?>

The above example will output:

array(4) {
array(4) {
  [0]=>
  string(3) "foo"
  [1]=>
  string(3) "bar"
  [2]=>
  string(5) "hello"
  [3]=>
  string(5) "world"
}
}

It is possible to specify the key only for some elements and leave it out for others:

Example #5 Keys not on all elements

<?php
$array = array(
         "a",
         "b",
    6 => "c",
         "d",
);
var_dump($array);
?>

The above example will output:

array(4) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [6]=>
  string(1) "c"
  [7]=>
  string(1) "d"
}

As you can see the last value “d” was assigned the key 7. This is because the largest integer key before that was 6.

Example #6 Complex Type Casting and Overwriting example

This example includes all variations of type casting of keys and overwriting of elements.

<?php
$array = array(
	1 => 'a',
	'1'  => 'b', // the value "a" will be overwritten by "b"
	1.5  => 'c', // the value "b" will be overwritten by "c"
	-1 => 'd',
	'01'  => 'e', // as this is not an integer string it will NOT override the key for 1
	'1.5' => 'f', // as this is not an integer string it will NOT override the key for 1
	true => 'g', // the value "c" will be overwritten by "g"
	false => 'h',
	'' => 'i',
	null => 'j', // the value "i" will be overwritten by "j"
	'k', // value "k" is assigned the key 2. This is because the largest integer key before that was 1
	2 => 'l', // the value "k" will be overwritten by "l"
);
var_dump($array);
?>

The above example will output:

array(7) {
  [1]=>
  string(1) "g"
  [-1]=>
  string(1) "d"
  ["01"]=>
  string(1) "e"
  ["1.5"]=>
  string(1) "f"
  [0]=>
  string(1) "h"
  [""]=>
  string(1) "j"
  [2]=>
  string(1) "l"
}
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Comment

Verified by MonsterInsights