PHP program to add item at the beginning of associative array Last Updated : 11 Feb, 2019 Comments Improve Suggest changes Like Article Like Report In PHP associative array is the type of array where the index need not to be strictly sequential like indexed array. Normally add a new element in an existing associative array it will get appended at the end of that array. Example: php <?php // Existing array $arr = array('one' => 1, 'two' => 2); // New element $arr['zero'] = 0; // Final array print_r($arr); ?> Output: Array ( [one] => 1 [two] => 2 [zero] => 0 ) So, a new element can not be added directly at the beginning of an associative array but the existing array can be appended at the end of a new array where the first element is the new element. It means add the new element in the beginning first the new element need to be put in an empty array as first element and then the array need to be merged with the existing array. In PHP, there are two ways to merge arrays they are array_merge() function and by using array union(+) operator. In case of array_merge() function if two arrays have a same key then the value corresponding to the key in later array in considered in the resulting array. But in case of indexed array the elements simply get appended and re-indexing is done for all the element in the resulting array. Syntax: array array_merge( $arr1, $arr2 ) In case of array union(+) operator if two arrays have same key then the value corresponding to the key in first array in considered in the resulting array, this also applies in the indexed array, if two array have an element of common index then only the element from first array will be considered in the resulting array. Syntax: $arr3 = $arr1 + $arr2 Program: PHP program to add a new item at the beginning of an associative array. php <?php // Adding a new element at the beginning of an array // Existing array $arr = array('one' => 1, 'two' => 2, 'three' => 3); // New element to be added at 'zero' => 0 // Create an array using the new element $temp = array('zero' => 0); // Append the $temp in the beginning of $arr // Using array union(+) operator $arr2 = $temp + $arr; echo "Result of array union(+) : "; print_r($arr2); // Using array_merge() function $arr3 = array_merge($temp, $arr); echo "\n" . "Result of array_merge() : "; print_r($arr3); ?> Output: Result of array union(+) : Array ( [zero] => 0 [one] => 1 [two] => 2 [three] => 3 ) Result of array_merge() : Array ( [zero] => 0 [one] => 1 [two] => 2 [three] => 3 ) Comment More infoAdvertise with us Next Article PHP program to add item at the beginning of associative array A Arghyadip_Manna Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads PHP Program to Sort an Array in Ascending Order Sorting array elements is a common operation in programming. PHP provides several methods to achieve this operation. In this article, we will explore various approaches to sort array elements of an array in ascending order.Table of ContentUsing sort() FunctionUsing asort() FunctionUsing array_multis 3 min read How to insert an item at the beginning of an array in PHP ? Arrays in PHP are a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data. The arrays are helpful to create a list of elements of similar types, which can be accessed 5 min read How to loop through an associative array and get the key in PHP? Associative arrays are used to store key-value pairs. For example, to store the marks of the different subject of a student in an array, a numerically indexed array would not be the best choice. Instead, we could use the respective subjectâs names as the keys in our associative array, and the value 3 min read How to Get All Keys of an Associative Array in PHP? Given an Associative array, the task is to get all the keys of the associative array in PHP. There are two approaches to get all keys from the associative array, these are - using array_keys(), and foreach loop. In this article, we will explore these approaches with examples.Table of ContentApproach 2 min read How to get numeric index of associative array in PHP? In PHP we can associate name/label with each array elements using => symbol. This is very helpful as it is easy to remember the element because each element is represented by the label rather than the index value. Using array_keys() function: The array_keys() function is an inbuilt function in PH 1 min read Program to Insert new item in array on any position in PHP New item in an array can be inserted with the help of array_splice() function of PHP. This function removes a portion of an array and replaces it with something else. If offset and length are such that nothing is removed, then the elements from the replacement array are inserted in the place specifi 4 min read How to access an associative array by integer index in PHP? There are two types of arrays in PHP, indexed and associative arrays. In case of indexed array strict numeric indexing is followed but in case of associative array there are keys corresponding to each element. The elements of an associative array can only be accessed by the corresponding keys. As th 3 min read How to remove a key and its value from an associative array in PHP ? Given an associative array containing array elements the task is to remove a key and its value from the associative array. Examples: Input : array( "name" => "Anand", "roll"=> "1") Output : Array ( [roll] => 1 ) Input : array( "1" => "Add", "2" => "Multiply", "3" => "Divide") Outpu 2 min read How to check an array is associative or sequential in PHP? In PHP there is no need to write the variable type before the variable because it is loosely-typed. It takes datatype from user defined values that are stored in it. Arrays in PHP is a type of data structure that allows to store multiple elements of similar data type under a single variable thereby 2 min read PHP Program to Move All Zeroes to End of Array Given an array of random numbers, Push all the zero's of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity i 3 min read Like