All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Get Value From Associative Array By Key

Last Updated : Mar 11, 2024

PHP Get Value From Associative Array By Key

In this tutorial we will show you the solution of PHP get value from associative array by key, associative arrays are same like indexed array but there is one main difference between them that is associative arrays used named keys that we assign to them.

For better understanding below is example or example of syntax of associative arrays.

$age=array("united_kingdom"=>"london",united_states"=>"washington","rusia"=>"moscow");

Step By Step Guide On PHP Get Value From Associative Array By Key :-

As, there are many ways with the help of which we can get value of associative array by key.

But here in this article we are going to discuss two main methods that are by using array_value() and other is using for loop.

Let us understand them one by one in given codes.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title> php get value of associative array by key </title>
</head>
<body>
 <?php
 $marks = array("Tony"=>"100", "Captain"=>"99", "Thor"=>"99", "Natasha"=>"32");
 print_r(array_values($marks));
 ?>
 //here is another way given as
<?php
$capital = array("Italy"=>"Rome", "China"=>"Beijing", "France"=>"Paris", "Korea"=>"Seoul");
foreach($capital as $key => $value)
{
echo $key . " : " . $value . "<br>";
}
?>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, as we can see that we create an array with four keys and their values. This all is store inside a variable named marks. After that we array_values to get the required output which we want. And at last we just use print_r to print the output.
  6. In next example, we again create an array with some keys and its values. This time we store our data inside $capital variable which is created using var.
  7. After that we use for loop and inside for loop. We display the values of associative array using keys. As you see in output.
  8. This all is done inside php tags. In basic structure of php <?php is the opening tag whereas ?> is closing tag. We hope that you understand the code properly.
  9. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to convert an object to array using JavaScript.

I hope this tutorial on PHP get value from associative array by key helps you and the steps and method mentioned above are easy to follow and implement.