Shivam
0
Q:

php add to associative array

// for php 5.4+
$data += [$key => $value];

// for php 5.4-
$data += array($key => $value);
1
<?php 
/* 
There are 3 Types of array in php  
1. Indexed arrays - Arrays with a numeric index
2. Associative arrays - Arrays with named keys
3. Multidimensional arrays - Arrays containing one or more arrays

This is the second one - Associative arrays
*/

$age = array("Samy"=>"35", "Naveen"=>"37", "Amit"=>"43");
echo "Mr.Samy is " . $age['Samy'] . " years old.";

?>
6
<?php


$person_with_age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

//echo "Peter is " . $person_with_age['Peter'] . " years old."; //just one

//now loop
foreach($person_with_age as $person => $age) {
  echo "Key=" . $person . ", Value=" . $age;
  echo "<br>";
}


?>
2
$a = array('foo' => 'bar'); // when you create
$a['Title'] = 'blah'; // later
0

New to Communities?

Join the community