php push to associative array code example

Example 1: php add to associative array

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

// for php 5.4-
$data += array($key => $value);

Example 2: add item to array in php

<?php

$a=array("red","green");

array_push($a,"blue","yellow");

print_r($a);
?>

Example 3: php add element to array

<?php
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
?>

Example 4: array_push in php

<?php
// Insert "blue" and "yellow" to the end of an array:


$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>

Example 5: php add to existing associative array

$a1=['aa'=>'123' , 'bb'=>'454'];

$a1 = array_merge( $a1 , ['a'=>1,'b'=>2] ) ;

Example 6: php add new item to associative array

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