Multidimensional arrays in php

In this tutorial learn how work array in php, In PHP, there are three types of arrays: Indexed/numberic arrays, Associative arrays and Multidimensional arrays in php.

Indexed arrays - In Arrays with a numeric index, value are stored and access in linearly. 

Associative arrays - In Arrays with named keys, value can stored associative with keys value also
Multidimensional arrays - In Arrays containing one or more arrays



Multidimensional arrays example
A multi-dimensional array each element in the main array can be an array also. Each element in the sub-array can be an array.

Example 1 

<?php
  
       $multi=array();
         $data=array();
         $store=array();
         $data[0] = "one";
         $data[1] = "two";
         $data[2] = "three";
         $data[3] = "four";
         $data[4] = "five";
  
         $store["one"] = "Jack";
         $store["two"] = "Taylor";
         $store["three"] = "Nakiya";
         $store["four"] = "Huge";
         $store["five"] = "Chirag";
         //create multidimensional array
         $multi[0]=$store;
         $multi[1]=$data;
       
         print_r($multi);

     

?>

Above Example Output:


Array ( [0] => Array ( [one] => Jack [two] => Taylor [three] => Nakiya [four] => Huge [five] => Chirag ) 
[1] => Array ( [0] => one [1] => two [2] => three [3] => four [4] => five ) )

Example 2

<?php
  
       $multi=array();
         $data=array();
         $store=array();
         $data[0] = "one";
         $data[1] = "two";
         $data[2] = "three";
         $data[3] = "four";
         $data[4] = "five";
  
         $store["one"] = "Jack";
         $store["two"] = "Taylor";
         $store["three"] = "Nakiya";
         $store["four"] = "Huge";
         $store["five"] = "Chirag";
         $store["six"]=$data; //store data array
         //create a multidimensional array
         $multi[0]=$store;
         //print array
         print_r($multi);

     

?>


Above Example Output:

Array ( [0] => Array ( [one] => Jack [two] => Taylor [three] => Nakiya [four] => Huge [five] => Chirag [six] => Array ( [0] => one [1] => two [2] => three [3] => four [4] => five ) ) )

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...