PHP ARRAYS-The Easy Way| techbit.in
PHP ARRAYS

What is a PHP Array?

PHP Array is simply a COMPLEX type of variable which is used to store multiple values at one time.

Note: The term “Complex” does not indicate the difficulty level here, rather it is used to emphasize the intricacy of an Array.

For example, If I have to make a PHP variable that denotes all the categories of Articles that Techbit has, then I would be making it one by one like this:

<?php
$techbit0 = "Cryptocurrency";
$techbit1 = "Programming";
$techbit2 = "Tips and Tricks";
?>

As you can clearly see here, this will become an exhausting code to write if I had 50 categories here. It also makes the code look lengthy and boring, so Array helps us in shortening it down, also making use of the DRY principle of programming.

<?php
$techbit_cat = array("Cryptocurrency", "Programming", "Tips and Tricks");
?>

And we can surely loop through the array to get the individual values.

Syntax of a PHP Array

PHP Array can be written using 3 different syntaxes:

<?php
$arr = array("one","two","three", 4);
?>
<?php
$arr = ["one","two","three", 4];
?>
<?php
$arr[0] = "one";
$arr[1] = "two";
$arr[2] = "three";
$arr[3] = 4;
?>

Types of PHP Array

PHP Arrays are of 3 different types:

  • Default / Numeric / Index-Based Array
  • Associative Array
  • Multi-dimensional Array

Default / Numeric / Index-Based PHP Array

It is the most basic type of array which incorporates an array element with an index value. The one we have discussed till now comes under an Indexed Array.

To print an indexed array

Primarily, if you want to print the complete array in detail, there are 2 ways for that:

First Method: To print the index information about the variable
<?php
$arr = array("one","two","three", 4);
print_r($arr);
?>
Output:
Array ( [0] => one [1] => two [2] => three [3] => 4 )
Second Method: To print the data type along with the index
<?php
$arr = array("one","two","three", 4);
var_dump($arr);
?>
Output:
array(4) { [0]=> string(3) "one" [1]=> string(3) "two" [2]=> string(5) "three" [3]=> int(4) }

Secondly, there are 2 different ways for printing a specific value of an array and also to print the complete array values.

First Method: To print a specific value of an array
<?php
$arr = array("one","two","three", 4);
echo $arr[2];
?>
Output:
three
Second Method: To print all the values of an array
<?php
$arr = array("one","two","three", 4);
for ($i=0; $i<=3; $i++)
{
echo $arr[i]." ";
}
?>
Output:
one two three 4

Associative PHP Array

Associative and Indexed Array share some common functionality. The major difference being that Associative arrays have a string replacing an index. It means that the information is stored in a key=>value format, wherein the key represents the category and the value denotes the content and it can be of any data type.

<?php
$techbit_info = ['cryptocurrency'=>'Dogecoin' , 'tips and tricks'=>'Bootable USB' , 'programming'=>'PHP'];
echo $techbit_info['tips and tricks']; 
?>
Output:
Bootable USB
Note: If a key is not assigned to any element, then the default value it gets is a 0.

Printing an Associative Array

<?php
$techbit_info = ['cryptocurrency'=>'Dogecoin' , 'tips and tricks'=>'Bootable USB' , 'programming'=>'PHP'];
foreach ($techbit_info as $key => $value) {
echo $key. " => ".$value.".<br>";
}
?>
Output:
cryptocurrency => Dogecoin.
tips and tricks => Bootable USB.
programming => PHP.

Multi-dimensional PHP Array

A multi-dimensional array is also known as a Nested Array. It is nothing but a more advanced version of an associative array.

It is a series of associative arrays one after the other, all looped into a single main array. Thus it is rightly called multi-dimensional since it can go up to 2, 3, 4, or more levels deep. We commonly encounter a nested array that is as deep as 3-4 levels. More complex programs often have a deeper level of nesting.

<?php
$multiDim = array( 
"Momo" => array (
             "age" => 2,
             "gender" => "Male",
             "city" => "Delhi"
),

"Sushi" => array (
             "age" => 8,
             "gender" => "Female",
             "city" => "Delhi"
)
);
echo "Sushi is a " . $multiDim['Sushi']['gender'].".";
?>
Output:
Sushi is a Female.

Printing a Multi-dimensional Array Array

A more practical yet complex example using nested Loops:
<?php
$matrix = array(array(10,20,30,40),
                array(50,60,70,80),
                array(90,10,11,12),
                array(13,14,15,16));

for ($i=0; $i < count($matrix); $i++) { 
  for ($j=0; $j < count($matrix[$i]); $j++) { 
      echo $matrix[$i][$j];
      echo " ";
  }
  echo "<br>";
}
?>
Output:
10 20 30 40
50 60 70 80
90 10 11 12
13 14 15 16

That’s all from my end. I hope it helped you somewhere in clearing your doubts regarding PHP Array. Please feel free to post any doubts/suggestions/feedback in the comments below. I’ll be happy to address it.

Kindly follow us on our social media handles too.

Check out my other programming-related articles here.


Vaishali Rastogi

Hey There! I am Vaishali Rastogi and I am a tech-researcher. I've been doing writing for a good 4-5 years, so here I am now. Working on my own website to make people digitally aware of the updates in technology.

2 Comments

Kirti · May 17, 2021 at 4:31 pm

Nice explanation Vaishali. More such articles on PHP, please.

Leave a Reply

Avatar placeholder

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

thirteen + 16 =