PHP Arrays

PHP Arrays.

Array is a memory location which is use to store multiple data with unique index in sequential memory location.
With any other programming language reference – aray is collection of similar data type but here in php we can store any type of data in a simgle array because in php we don’t have need to declare data type before array initialization.

Types of Arrays in PHP

There are three types of arrays that you can create. These are:

  • Numeric array — An array with a numeric key.
  • Associative array — An array where each key has its own specific value.
  • Multidimensional array — An array containing one or more arrays within itself.

Numeric Array

These arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero.
Index Rules of Numeric Array.

  1. Index with a numeric key.
  2. Index starting from zero.
  3. Index always in assending order.
  4. no of index = no of value – 1

Example of Numeric Array
This example showing how to create and access numeric arrays.
Here we have used array() function to create array.

<?php
    /* First method to create array. */
    $numbers = array( 1, 2, 3, 4, 5);
      
    foreach( $numbers as $value ) {
       echo "Value is $value <br />";
    }
         
    /* Second method to create array. */
    $numbers[0] = "one";
    $numbers[1] = "two";
    $numbers[2] = "three";
    $numbers[3] = "four";
    $numbers[4] = "five";
         
    foreach( $numbers as $value ) {
        echo "Value is $value <br />";
    }

?>

Associative Arrays

The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index.
In this array we can assign index name according to it’s corresponding value and our need. In this association are have use associative array operator ( => ) in associative array index cannot follow any type of order but whenever default array will be found it will work like a default array.

To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.

NOTE − Don’t keep associative array inside double quote while printing otherwise it would not return any value.

Example of Associative Arrays

<?php
     /* First method to associate create array. */
     $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);
        
     echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
     echo "Salary of qadir is ".  $salaries['qadir']. "<br />";
     echo "Salary of zara is ".  $salaries['zara']. "<br />";
         
     /* Second method to create array. */
     $salaries['mohammad'] = "high";
     $salaries['qadir'] = "medium";
     $salaries['zara'] = "low";
         
     echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
     echo "Salary of qadir is ".  $salaries['qadir']. "<br />";
     echo "Salary of zara is ".  $salaries['zara']. "<br />";
?>

Multidimensional Arrays

A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.

Example of Multidimensional Arrays
In this example we create a two dimensional array to store marks of three students in three subjects − This example is an associative array, you can create numeric array using same things.

<?php
    $marks = array( 
    "mohan" => array (
    "physics" => 35,
    "maths" => 30,    
    "chemistry" => 39
    ),

    "rahul" => array (
    "physics" => 30,
    "maths" => 32,
    "chemistry" => 29
    ),
            
    "sanjay" => array (
    "physics" => 31,
    "maths" => 22,
    "chemistry" => 39
    )
    );
         
    /* Accessing multi-dimensional array values */
    echo "Marks for mohan in physics : " ;
    echo $marks['mohan']['physics'] . "<br />"; 
        
    echo "Marks for rahulin maths : ";
    echo $marks['rahul']['maths'] . "<br />"; 
         
    echo "Marks for sanjayin chemistry : " ;
    echo $marks['sanjay']['chemistry'] . "<br />"; 
?>
   

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Free online tool to download Instagram videos, photos and save Instagram videos, photos to your device. It's free & easy to use.

Download Now

CURRENCY CONVERTER

Our real time currency converter calculator will convert your money based on current values from around the world. It's free & easy to use.

Convert Now

ROBOST.TXT GEN.

Create robots txt file that help search engines to crawling and indexing your site with help of our free online Robots.txt Generator tool.

Create Now

HTML MINIFIER

Free online tool to minify or compress html code and reduce the size of html code in few clicks. It's free & easy to use.

Minify Now

CSS MINIFIER

Free online tool to minify or compress css code and reduce the size of css code in few clicks. It's free & easy to use.

Minify Now

JAVASCRIPT MINIFIER

Free online tool to minify or compress javascript code and reduce the size of javascript code in few clicks. It's free & easy to use.

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.