What is an array in PHP

Here you will learn what is an array in PHP. But, before you understand what an array is, you must first learn PHP variables.

What is an array in PHP?

A PHP array is a container like PHP Variables. In a PHP Variable, you can store only one value at once. But, In a PHP array, you can store multiple values at once. That means an array is used to store multiple values at a time.

हिंदी | PHP array in Hindi


How to Declare or Create an array?

In PHP, you can create an array in two ways – 1. using array() function, 2. using Square Brackets [].

The array values are separated by a comma ( , )

Creating an array using the array() function.

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

Creating an array using square brackets [].

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

Both array() and square brackets do the same work. The square bracket is the shorthand of the array() function.


How to access the array values?

You can access array values ​​by their keys. But, how to find out their keys. By default, PHP gave them keys in number format (starting with 0).

PHP Indexed or Numeric Array
<?php
//                0      1       2     3      4
$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

How to add custom array keys?

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

PHP Associative Array
<?php
$data = array('name' => 'John', 'age' => 21, 'email' => '[email protected]');
echo $data['name'];
echo "<br>";
echo $data['age'];
echo "<br>";
echo $data['email'];
?>

Browser Output

PHP array types

PHP has two types of arrays –

  1. Indexed arrays (Numeric arrays)
  2. Associative arrays

PHP Indexed or Numeric Arrays

Indexed or Numeric arrays are those arrays whose keys are assigned automatically by PHP, and the keys are in number format (start with 0).

$names = array('John','Smith','Mark','Ken','Barry');

$names2 = ['John2','Smith2','Mark2','Ken2','Barry2'];

PHP Associative Arrays

हिंदी | PHP Associative Array Tutorial in Hindi

PHP Associative arrays are those arrays whose keys are assigned by you.

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

$data2 = ['name2' => 'John', 'age2' => 21, 'email2' => '[email protected]'];

Multidimensional Arrays in PHP

हिंदी | PHP Multidimensional Array in Hindi

When you create an array or arrays (these arrays can be Indexed or Associative) inside an array it is called a multidimensional array.

<?php
$school = array(
    'teachers' => array('John','Mark','Smith'),
    'subject' => array('English','Science','Mathematics')
);

// $school is an array
// $school['teachers'] is an array which is inside the $school array
echo $school['teachers'][0]; //John
echo "<br>";

// $school['subject'] is another array which is inside the $school array
echo $school['subject'][1]; //Science
?>
Browser Output
John
Science

Tuff example of Multidimensional Array

<?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')
    )
);
?>

Leave a Reply

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