key value array for php code example

Example 1: php for array key value

<?php
$array = array(1=>"apple",2=>"Orange",3=>"Banana");

foreach($array as $key => $value){
	echo "The value for " . $key . " is " . $value;
}
/*The value for 1 is apple
The value for 2 is Orange
The value for 3 is Banana

$array = [1 => "apple", 2 => "Orange", 3 => "Banana"    
    Is also valid
];*/  
?>

Example 2: array key value php

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
foreach($age as $x=>$x_value)
  {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
  }
?>

Tags:

Php Example