Associative 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



The Associative arrays and Indexed array are similar in term of functionality but they different in term of their key index.


Associative arrays Example: 



$data = array("one" => "Jack","two" => "Taylor","three" => "Nakiya","four" => "Huge","five" => "Chirag"); 

Or store in manually also 


 $data["one"] = "Jack";
 $data["two"] = "Taylor";
 $data["three"] = "Nakiya";
 $data["four"] = "Huge";
 $data["five"] = "Chirag";
 print_r($data);

      
Above example OUTPUT: 

Array ( [one] => Jack [two] => Taylor [three] => Nakiya [four] => Huge [five] => Chirag )


To loop through and print all the values of an indexed array, you could use a foreach loop, like this:


<?php
 $data["one"] = "Jack";
 $data["two"] = "Taylor";
 $data["three"] = "Nakiya";
 $data["four"] = "Huge";
 $data["five"] = "Chirag";
  
 foreach( $data as $key => $value ) {
            echo "data key ".$key." value is ".$value."<br />";
         }     
      

?>
Above Example Output:


data key one value is Jack
data key two value is Taylor
data key three value is Nakiya
data key four value is Huge
data key five value is Chirag

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...