array associative php code example
Example 1: Associative array in php
<?php
$age = array("Samy"=>"35", "Naveen"=>"37", "Amit"=>"43");
echo "Mr.Samy is " . $age['Samy'] . " years old.";
?>
Example 2: how to create an associative array in php
<?php
$associativeArray = [
"carOne" => "BMW",
"carTwo" => "VW",
"carThree" => "Mercedes"
];
echo $associativeArray["carTwo"] . " Is a german brand";
?>
Example 3: how to create associative array in php
<?php
$a = array('a','b','c');
$b = ['a','b','c'];
$c = array(
'keyOne'=>'valueOne',
'keyTwo'=>'valueTwo'
);
$d = [
'keyOne'=>'valueOne',
'keyTwo'=>'valueTwo'
];
$c = array(
'keyOne'=>array('a','b','c'),
'keyTwo'=>array('a'=>'1','b'=>'2')
);
$d = [
'keyOne'=>['a','b','c'],
'keyTwo'=>['a'=>'1','b'=>'2']
];
?>
Example 4: array associativo php
<?php
$age = array("Samy"=>"35", "Naveen"=>"37", "Amit"=>"43");
echo "Mr.Samy is " . $age['Peter'] . " years old.";
?>
Example 5: php define variables from array associative
<?php
$array = [
'key1' => 'foo',
'key2' => 'bar',
];
extract($array);
echo $key1;
echo $key2;
?>
Example 6: php associative array
<?php
$arr = array('fruit' => 'mango', 'vegetable' => 'tomato', 'thing' => 'bag');
echo $arr['fruit']
mango
?>