In this tutorial we will show you the solution of PHP array push with key, usually associative array means an array with string as an index. Rather than storing element values in a strict linear index order, it stores them in combination with key values.
Here we need to push some values with key to an array with some string as values by using inbuilt method in php like in json array.
The array_push() method used to we can insert combination of key
with values to an array with some strings as a array values.
Step By Step Guide On PHP Array Push With Key :-
For inserting key with values to an array we used array_push() method. The array_push() method inserts one or more elements to the end of an array. So we can add one values, or mony as we like.
Here we used this for insert value with key of array to an array with collection of string as values only.
Key means denotes indexes. Result will display on plain webpage because we are not styled.
<?php $arr=array("kavi","abi"); array_push($arr,["country"=>"delhi"]); print_r($arr); ?>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?> tags.
- The default file extension for php files is “.php” and php each statements end with ‘;’ semicolon. Between start and end tag we need to define our program in php.
- We declared one array with two string values that array variable name is ‘$arr’ and here for declare one variable we need to use ‘$’ symbol before variable name.
- In array values are ‘“kavi”,”abi”’ then before defining values ‘array’ a word mentioning is must because it denotes as a array values. When we pushing some value with key means use “=>” symbol points respective values.
- In array we can define with any number of values, those are our choice not mandatory to use same as above used values and result needs to have all described values in array.
- Using ‘array_push()’ method with two parameters is must first one defines array variable name where to append our key with value of array and next we defined (key is ‘country’, value is ‘delhi’) as input passed it returns all values with array present in array as a result displayed on webpage.
- In php ‘print_r()’ method is an inbuilt function that is used to print or display the information stored in a variable. Basically it prints human-readable information about a variable. If the variable is a string, integer or float the value itself will be printed.
- With the help of ‘Print_r()’ function used to displayed all values on browser.
Conclusion :-
In conclusion we are able to know how to push key with value to an array using php.
When work with php we need to create and process php files at server location and then we need to start the server before execute the program.
When executing this program on browser page, we defined array ‘$arr’ with two string values, our pushed keys with value of array will display as a result.
Array_push() method always push element values to the end of an array.
I hope this article on PHP array push with key helps you and the steps and method mentioned above are easy to follow and implement.