PHP Array

An array is a special variable which can store multiple values.

    <?php    
        //array is creating by following function
        array();
    ?>

Array Types

There are three types of arrays in PHP:

  • Indexed Array
  • Associative Array
  • Multidimensional Array

Indexed Array

Indexed array is an array with a numeric index

    <?php 
        $users = array('User 1', 'User 2', 'User 3');
        //or (both are same)
        $users = ['User 1', 'User 2', 'User 3'];
    ?>

Associative Array

Indexed array is an array with named keys

    <?php 
        $user = array(
                    'name' => 'User 1', 
                    'email' => 'user1@gmail.com', 
                    'mobile_number' => '1111111111'
                );
                
        //or (both are same)

        $user['name'] = 'User 1';
        $user['email'] = 'user1@gmail.com';
        $user['mobile_number'] = '1111111111';
    ?>

Multidimensional Array

Indexed array is an array containing one or more arrays

    <?php 
        $users = array(
                    'user1' => array(
                                'name' => 'User 1'
                            ), 
                    'user2' => array(
                                'name' => 'User 2'
                            ), 
                );
    ?>
PHP Tutorial