All TalkersCode Topics

Follow TalkersCode On Social Media

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

PHP Get Array Value By Key

Last Updated : Mar 11, 2024

PHP Get Array Value By Key

In this tutorial we will show you the solution of PHP get array value by key, as we know that array is used to store values or data of same datatype to a single variable.

In php, it is very easy to create an array and assigns values to arrays. Now, let us understand how we can get array value by key.

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

Here, as we know that array is of three types. Its first type is indexed array, next one is associative array and last one is multidimensional array.

Indexed array and associative arrays have a minor difference of key whereas multidimensional array is array of array.

Let us see how to get array value by key in php that can be used in all types.

<!DOCTYPE html>
      <html>
        <head>
          <title> php get array value by key </title>
   </head>
 <body>
 <?php
 $array = [
 [1,5,10,65,80,23,59],
 [47,89,32,02,45,45,20],
 [10,20,904,45,20,36,578]
 ];
 foreach($array as $key => $value)
{
 if(isarray($value)){
 foreach($value as $key)
 {
 echo $key;
 }
 }
}
 ?>
 </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, inside body as we can see that there we create a php tag. In which <?php is our opening tag and ?> is our closing tag.
  6. In these tags we will write our php code. Here, as we can see that first of all we create an array. and we store this array in $array variable. After this we have to get array value by key. This is an example of multidimensional, indexed array.
  7. Whereas answer is given in all forms. For answer, there is no meaning that what type of array it is.
  8. Now, we use for loop here. if you don’t know about for loop then you can visit to our article in which we understand about for loop.
  9. Mainly for loop is use in case of associative arrays. So, to describe answer of associative arrays, we use here for loop.
  10. Next we use here isarray to check further array for multidimensional array. If it is not a multidimensional array then we can skip it.
  11. At last we use echo to print the values of full array using key. We hope you understand codes easily.
  12. 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 get array value by key.

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

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪