PHP Tutorial #01 Indexed Arrays, Associative Arrays and Multidimensional Arrays

Words
674
Reading
3 min
Listen
Play
8y

Image Source

Repository

https://github.com/php/php-src

What Will I Learn?

I will learn the arrays , I will cover 3 types of arrays indexed arrays, associative arrays and multidimensional arrays with examples .

  • What's the Indexed Arrays and how to use it.
  • The definition of Associative Arrays and  their uses. 
  •  The Multidimensional Arrays.

Requirements

  • Server support PHP , Xampp or Wamp for example
  • An IDE like Sublime text.
  • Browser (Chrome for example)

Difficulty

  • Basic

Description

In this tutorial we will start the PHP programming language, we will talk about the arrays ,the types of arrays and the difference between them.

Introduction To Arrays :

Arrays are types of structured data that group information together. The information can be in many types (integers, reals, floats, Booleans, strings ..etc), arrays can store one or more values at a time and the values can be different ( integer with string for example ). 

When declaring a table, it is useless to specify its size and the type of data it will contain. PHP does it automatically. The arrays are dynamic tables. At each new entry saved in the table, PHP expands its size by 1 element. There is three types of arrays: numeric index arrays, associative arrays and multidimensional arrays.  

1 - Indexed Arrays :

An indexed array is simply a list of elements each identified by a unique numeric index. The first element of the array will be indexed by index 0, the second by index 1, the third by index 2, and so on. The indexes are automatically set by PHP.  

To declare an indexed array we use this syntax : 

$name = array(value1, value2, value3 ..etc);

In my example I will declare an array of friends , the type of values that will contain is "String" 

$Friends = array(

"Alexendre",

"Alex",

"Aldwin",

"Arden",

"Mick"

);

To show the array you have many ways, you can use the print_r($array) method, you can also use a for loop or foreach loop ..etc

for ($i = 0; $i < $nub; $i++ ) {

     echo $Friends[$i] . " <br/>";

}

print_r() method :

echo "<pre>";

print_r($Friends);

echo "</pre>";

I have used two methods and this is the result of each one : 

2- Associative Arrays :  

Associative arrays work on the same principle, except that instead of numbering the boxes, we will label them by giving each a different name. 

To declare an indexed array we use this syntax : 

$name = array(

   key => value 

);

The difference between the associative and the indexed arrays is the keys, in the indexed arrays the PHP give for each value an index, in the associative array that's you who give each value a key.

$languages = array(

"en" => "English",

"fr" => "French",

"es" => "Spanish",

"pt" => "Portuguese"

);

You have many ways to show the values of the associative arrays, let's use the print_r method again and the foreach loop.

echo "<pre>";

print_r($languages);

echo "</pre>";

Foreach Loop :

foreach ($languages as $lang => $langname) {

echo "[" . $lang . "] is the key of " . $langname . "<br/>";

}

And this is the result :

3-  Multidimensional Arrays : 

The multidimensional array is an array contains one or more arrays , the keys of this array can be automatically from 0 to the last item , or by giving each a different name as associative array .

To declare an indexed array we use this syntax : 

$name = array(

    array(

             array(),

             array()

    ),

    array()

);

The main array contains many other arrays , it will be multidimensional array more than 2D.

$materials = array( //associative array

"Phones" => array(//associative array

"Samsung" => array ( //indexed array

"Samsung S",

"Samsung Note",

"Samsung G"

),

"iPhone" => array ( //indexed array

"iPhone SE",

"iPhone 8",

"iPhone X"

),

)

);

I will use the foreach and print_r to show the values of this array and this is the result :

Video Tutorial

Curriculum

This is the first video in this tutorial . 

Proof of Work Done

https://github.com/alexendre-maxim/PHP-Tutorial

PHP Tutorial #01 Indexed Arrays, Associative Arrays and Multidimens... | Ecency