Advertisements
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:
- How to check if a key exists in an array in PHP
- How to check if a value exists in an array in PHP
- How to get all the keys of an associative array in PHP
- How to get all the values from an associative array in PHP
Advertisements