About Me

header ads

Array

Count Array

  

Input

echo ' $cars=array("Volvo","BMW","Toyota"); echo count($cars); ';
  

  echo"

Output

"; $cars=array("Volvo","BMW","Toyota"); echo count($cars);

Decide String and numbers inside Array

Input

echo ' $cars=array("Lamborghini","BMW","aston martin" ,1,20,30.2); print_r($cars); foreach ($cars as $key => $value) { echo " $value=>".gettype($value)"; }';
  

   echo"

Output

"; $cars=array("Lamborghini","BMW","aston martin",1,20,30.2);

Printing direct array Values( Indexed Array )

print_r($cars);

Printing value and Its it's Type

foreach ($cars as $key => $value) { echo "$value => ".gettype($value)."
"; }

Array Types

Input

echo '

Associative Array

$cars=array("Favourite"=>"Lamborghini","BMW" ,"aston martin",1,20,30.2); print_r($cars); }

Indexed Array

Multidimensional Array

$cars=array("Favourite"=>array(" Multi-dimentional 1" =>"Lamborghini"," Multi-dimentional 2 "=>"Pulsar") ,"indexed"=>"BMW","aston martin",1,20,30.2); print_r($cars); ';


 echo"

Output

"; $cars=array("Favourite"=>"Lamborghini","BMW","aston martin",1,20,30.2);

Associative Array

print_r($cars); $cars=array("Favourite"=>array("Lamborghini","Pulsar") ,"indexed"=>"BMW","aston martin",1,20,30.2);

Multidimentional Array

print_r($cars); echo $cars [0][1]; echo "

//problem found Why its coming 'S'instead of Pulsar code given below in comments

";

Array Conditions[ Exact Values ]

Input

echo ' $cars=array(1,20,30.2,92); foreach ($cars as $key => $value) { if ($key<=1) { echo $value; } else{ die(); } } '; echo "

//problem found: Can i Use while loop inside of Foreach ?

";


 echo"

Output

"; echo "

printing Exact 2 values from Array

"; $cars=array(10,20,30.2,40,50,60,70,80,90,100); echo "Type 1
"; foreach ($cars as $key => $value) { // while ( $key <= 10) { // echo "string"; // } if ($key<=1) { echo $value."
"; } else{ // die(); // print_r($value)."
else"; } }

Post a Comment

0 Comments