php arrays

Jan 11, 2016

Milan Sanathara



php arrays

This blog will help you to understand the basic structure of an array and know how of an array with php array tutorial examples.

While,doing the programming ,so many times you need to create an array.So, what is an array exactly ? and why it requires?

What is an array?

In general, an array is a number of items arranged in some specified way.

An array is a data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer or string. Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched.

It works on the principle of key value combination.So that each value is stored in a specific cell with an unique key.

php arrays tutorial examples

php array tutorial examples

Consider, the above image, the array of programming languages, having keys 0,1,2,3,4 and each has some value ,such as php,java,C#.

The keys are unique identities of certain values.

Array In Php.

There are mainly three types of an array in php.

  • Numeric array
  • Associative array
  • Multidimensional array

Consider following php array tutorial examples.

Numeric Array

An array with a numeric index. Values are stored and accessed in linear fashion.

$indexed_array = array();
$indexed_array[0] = "Php";
$indexed_array[1] = "Java";
$indexed_array[2] = "Python";
$indexed_array[3] = "Perl";
$indexed_array[4] = "c#";


If you have an indexed array having key 0,1,2,3,4 then you can print any element with key.To get any certain out put,you need to write down following code.

                   echo $indexed_array[0];
                   O/P : Php //

You can also use the for loop to print the data from an array.

       foreach($indexed_array as $value){
           echo $value ."<br/>";
php arrays

php array

 Associative Array

$named_array = array();
$named_array['php'] = "PHP Hypertext Processor.";
$named_array['java'] = "Developed by Sun Microsystems in the 1990s.";
$named_array['python'] = "Python works on a core philosophy";
$named_array['perl'] = "Perl is a family of high-level.";
$named_array['c#'] = "well compiled and case sensitive web programming.";


Iteration of Array

      echo $named_array['php];
      O/P : PHP Hypertext Processor.//

Or , You can use the foreach loop.which returns with all printed elements of a given array.

    foreach ($named_array as $value) {
        echo $value . "<br>";
php arrays

php array

Multidimensional Array

An array containing one or more arrays and values are accessed using multiple indices.

$multi_dimen_array = array();
$multi_dimen_array[] = array(
    "apple" => "Red",
    "banana" => "Green",
    "pineapple" => "Yellow",
    "palm" => "Blue/Purlpe"
$multi_dimen_array[] = array(
    "beet" => "Red",
    "broccoli" => "Green",
    "pumpkin" => "Yellow",
    "eggplant" => "Blue/Purlpe"

php arrays

php array

Iteration of Array

Now, consider if you have an multidimensional array as shown above,then you need to use foreach loop, To get data from an array.

foreach ($multi_dimen_array as $key => $value) {
    foreach ($value as $sub_key => $sub_val) {
        echo $sub_key . " - " . $sub_val . "<br>";
    echo "<hr>";

Kindly,visit our blog regularly for learn more Slim Tutorial.
I hope you will find this post very useful regarding php and php arrays. Let me know any questions if you have in comment regarding php arrays. I will reply you ASAP.

Have you got cool idea about Web Development ? Contact us Now to get free consultation on your idea.  Alphanso Tech is rated as one of the best Php Development Company in India.

Post by Milan Sanathara

I am iOS Application Developer with an aspiration of learning new technology and creating a bright future in Information Technology.

One Response to php arrays

  1. Pingback: php tutorials with examples | Step by Step tutorial guide

Leave a Reply

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