Associative Array In PHP
Last Updated : Jul 1, 2023
IN - PHP
In this article we will show you the solution of associative array in php, usually we use array when needs to hold more than two values on one variable whereas associative array helps to store several array values.
Therefore, we must need to learn this topic isn’t it? Yes, First you should define a associative array variable like creating variable in php.
At the right hand side, to declare values everyone need to follow some rules that pre-defined definition.
By doing this leads to get error-free result and helps to learn this topic clearly.
Step By Step Guide On Associative Array In PHP :-
As foremost step, we need to know syntax of associative array with initialized values.
Here give you syntax for shown example that consists one array with multiple keys and values.
As per above given creating array lead you obtain successful creation.
In this array, you can retrieve particular key or value through specific key or value. As well as you can print values by using loop later we will see that coming lessen.
<?php $a=array("Dharani"=>"28","Prawin"=>"29","Arun"=>27); echo "In associative array values arun age is ".$a['Arun']. " years old"; ?>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
- The default file extension for php files is “.php” and php statements end with ‘;’ semicolon. In php we need to specify ‘$’ symbol before the variable name also default rule.
- Firstly we defined array variable ‘$a’ then initialized array values with three different sets ‘("Dharani"=>"28","Prawin"=>"29","Arun"=>"27")’.
- Instead of these values define another set of values as your wish that not affect your result. Using echo statement here we retrieved particular value by specify key ‘Arun’ with text ‘In associative array values arun age is years old’.
- For $a[‘Arun’], if you give as $a[‘Prawin’] in this program you will get result 29 instead 27. You can check this by modify above provided sample with this value and execute it. One more thing is your definition should have to similar without a tiny change to attain flawless result.
- Instead echo() you can prefer print() method if you want and that not going to affects your result. Still some type of input exist you can try those along with it to master your mind in this concept.
In conclusion, hope now you know how to create associative array with sample values.
When you 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.
Otherwise sometimes you will get error so ensure it if you get any error while running output.
When we execute this program on browser it will print ‘In associative array values arun age is 27 years old’ as output.
With this help of sample code you can test with other values and you will get result according to your values change.
I hope this article on associative array in php helps you and the steps and method mentioned above are easy to follow and implement.