How to Push Both Key and Value Into an Array in PHP
Topic: PHP / MySQLPrev|Next
Answer: Use the Square Bracket []
Syntax
You can simply use the square bracket []
notation to add or push a key and value pair into a PHP associative array. Let's take a look at an example to understand how it basically works:
Example
Try this code »<?php
// Sample array
$array = array("a" => "Apple", "b" => "Ball", "c" => "Cat");
// Adding key-value pairs to an array
$array["d"] = "Dog";
$array["e"] = "Elephant";
print_r($array);
?>
Related FAQ
Here are some more FAQ related to this topic: