What is Array?Types of arrays in php.

An array is a variable which stores multiple values in one single variable .However it only stores one data type value i.e when you stores name of student into an Array But you can’t store number value with strings.The indexing of array by default starting from 0 to n-1 where n = number of elements in arrays

Create an array in php

So In php we can create the array by using php in-built function array();

below is the example how we can create array in php and how to access the array value:

<?php
  //create an array
  $name = array('abhishek','vishal','ankur');
  // to access the arrays value ..
  echo $name[0]."<br>";
  echo $name[1]."<br>";
  echo $name[2];
?>

Their are three types of Arrays in php:

  1. INDEXED ARRAY
  2. ASSOCIATIVE ARRAY
  3. Multidimensional Array

Indexed Array

By default in php array are indexed type whenever you create an array like above example as you will be seeing.

So indexed array we can create indexed array in two ways as you previously show above but below is another way to create indexed array

<?php  
  //create an indexed array named student
  $student[0] = 'Abhishek';
  $student[1] = 'Rahul';
  //finding the length of array
  $length = count($student);
  for($i=0;$i<$length;$i++) {
     echo $student[$i];
  }
?>

Associative Array

Associative arrays uses the concept of key-value pair. An associative array is similar to an indexed array.but rather than storing data sequentially with numeric index, every value can be assigned with a user-designed key of string type.

the associative array also created by two ways

<?php
//first way to create associative araays..
  $age['ram'] = 30;
  $age['abhishek'] = 12;
  $age['rahul'] = 22;
//find the length of array....
  $length = count($age);
// getting the array of keys/index strings
  $keys = array_keys($age);
  for($i=0;$i<$length;$i++) {
     echo 'Age of '.$keys[$i].' is: '$age.[$keys[$i]]."<br>";
  }
?>

second ways like this

<?php
  $age = array("abhishek"=>22,"ankur"=>25,"rahul"=>33);
//find the length of array....
  $length = count($age);
// getting the array of keys/index strings
  $keys = array_keys($age);
  for($i=0;$i<$length;$i++) {
     echo 'Age of '.$keys[$i].' is: '$age.[$keys[$i]]."<br>";
  }
?>

In above code we used array_keys($arr); function which is used to access the key defined in array .in array_key() method take parameter only array type.

Multidimensional Array

Array elements in PHP can hold values of any type, such as numbers, strings and objects. They can also hold other arrays, which means you can create multidimensional, or nested, arrays.

Syntax for creating multidimensional array

<?php
/* 
    multidimensional array initialization
*/
$cars = array(
    array(
        "name"=>"Abhishek", 
        "age"=>"22", 
        "city"=>"haridwar"
    ),
    array(
        "name"=>"Shubham", 
        "age"=>"25", 
        "city"=>"Delhi"
    ),
    array(
        "name"=>"Amitabh", 
        "age"=>"29", 
        "city"=>"Mumbai"
    ),
);
?>

Accessing elements of multidimensional array

To access multidimensional array elements, you can use the same  square bracket [ ]that you use with regular arrays. If you want to access the second-level array elements in a 2-dimensional array, just use 2 sets of square brackets .so below code we can see how we can access elements

<?php
/* 
    multidimensional array initialization
*/
$cars = array(
    array(
        "name"=>"Abhishek", 
        "age"=>"22", 
        "city"=>"haridwar"
    ),
    array(
        "name"=>"Shubham", 
        "age"=>"25", 
        "city"=>"Delhi"
    ),
    array(
        "name"=>"Amitabh", 
        "age"=>"29", 
        "city"=>"Mumbai"
    ),
);
//find the length of array
$length = count($cars);
$keys =array_keys($cars);
// using the for loop
for($i=0;$i<3;$i++) {
    foreach($cars[$keys[$i]] as $key => $value) {
        echo $key . " : " . $value . "<br>";
    }
    echo "<br>";
}

?>
0 0 vote
Article Rating

Subscribe
Notify of
guest
2 Comments
oldest
newest most voted
Inline Feedbacks
View all comments
2
0
Would love your thoughts, please comment.x
()
x