What is PHP Array? Explained for Beginners

PHP

A PHP array is a container where you can store multiple values ​​at once. It is like a PHP variable but, in a variable, you can store only one value at a time, where an array can hold one or more than one value.

Create a PHP Array

⏳ You can create a PHP array in two ways –

  1. using the array() function
  2. using Square Brackets []

Both ways work the same. The square bracket is the shorthand of the array() function.

<?php
  $names = array('John','Smith','Mark','Ken','Barry');
?>
<?php
  $names = ['John','Smith','Mark','Ken','Barry'];
?>

You can store values ​​of any data type in an array, such as string, number, boolean, etc. And it is not mandatory that all values ​​of an array be of the same data type.

<?php
  $data = array('John', 21, true, NULL);
?>

Access the Array Values

You can access the values of an array ​​by their keys. By default, each array has keys in a number format starting at 0.

PHP array Keys
<?php
  $names = array('John','Smith','Mark','Ken','Barry');

  echo $names[0]; // John
  echo "<br>";
  echo $names[3]; // Ken
  echo "<br>";
  echo $names[4]; // Barry
  echo "<br>";
  echo $names[2]; // Mark
  echo "<br>";
  echo $names[1]; // Smith
?>
Browser Output
John
Ken
Barry
Mark
Smith

Add Custom Array Keys

If you want to add custom array keys, then you can do that by using the arrow operator =>

Custom Array Keys
<?php
  $user = array('name' => 'John', 'age' => 21, 'email' => '[email protected]');

  echo $user['name'];
  echo "<br>";
  echo $user['age'];
  echo "<br>";
  echo $user['email'];
?>
Browser Output

PHP Array Types

PHP has three types of arrays –

  1. Indexed arrays (Numeric arrays)
  2. Associative arrays
  3. Multidimensional Arrays

Indexed or Numeric Arrays

Arrays with numeric keys are called Indexed or Numeric arrays.

<?php
  $names = array('John','Smith','Mark','Ken','Barry');
?>

Associative arrays

Arrays with custom keys are called Associative arrays.

<?php
  $data = array('name' => 'John', 'age' => 21, 'email' => '[email protected]');
?>

Multidimensional Array in PHP

When you create an array or arrays inside an array, it is called a multidimensional array. These arrays can be Indexed or Associative.

<?php
  $school = array(
    'teachers' => array('John','Mark','Smith'),
    'subject' => array('English','Science','Mathematics')
  );
?>
<?php
  $school = array(
      'teachers' => array(
          'english' => array('John','Mark','Smith'),
          'science' => array(
              'physics' => array('Hank','Joe'),
              'chemistry' => array('Akash','Niels'),
              'biology' => array('James', 'Theressa')
          ),
          'Mathematics' => array('Barry','Nick')
      )
  );
?>

Update and Delete an Array in PHP

Update an array element/value

First, select the element by the key of the element, and then assign it to a new value that will replace the old value.

<?php
  $names = ['John','Smith'];

  // Updating John -> Mark
  $names[0] = 'Mark';

  echo "<pre>";
  var_dump($names);
  echo "</pre>";
?>
Browser Output
array(2) {
  [0]=>
  string(4) "Mark"
  [1]=>
  string(5) "Smith"
}

Add an Element inside an Array

With the help of the array_push() function, you can add an Element inside an array.

<?php
$users = array("John","Mark");

array_push($users, "Barry");

echo "<pre>";
var_dump($users);
echo "</pre>";
?>
Browser Output
array(3) {
  [0]=>
  string(4) "John"
  [1]=>
  string(4) "Mark"
  [2]=>
  string(5) "Barry"
}

You can also add multiple elements at once with the help of this function.

<?php
$users = array("John","Mark");

array_push($users, "Barry", "Akash", "Abhi");

echo "<pre>";
var_dump($users);
echo "</pre>";
?>
Browser Output
array(5) {
  [0]=>
  string(4) "John"
  [1]=>
  string(4) "Mark"
  [2]=>
  string(5) "Barry"
  [3]=>
  string(5) "Akash"
  [4]=>
  string(4) "Abhi"
}

Add an element with custom key

If you want to add an array element with a custom key, see the following example –

<?php
$users = array("John","Mark");

$users["name"] = "Barry";
$users["age"] = 25;

echo "<pre>";
var_dump($users);
echo "</pre>";
?>
Browser Output
array(4) {
  [0]=>
  string(4) "John"
  [1]=>
  string(4) "Mark"
  ["name"]=>
  string(5) "Barry"
  ["age"]=>
  int(25)
}

Delete an array element and an array

If you want to delete an array or want to delete an array element, in both of the cases you can use the unset() function.

<?php
$users = array("John","Mark");

// Deleting First Element
unset($users[0]);

echo "<pre>";
var_dump($users);
echo "</pre>";
?>
Browser Output
array(1) {
  [1]=>
  string(4) "Mark"
}
<?php
$users = array("John","Mark");


// Deleting the full array
unset($users);
?>
Tags

You May Also Like

Leave a Comment

Your email address will not be published. Required fields are marked *