You are here: Home / Topics / How to get a value from an array in PHP

How to get a value from an array in PHP

Filed under: PHP on 2023-07-02 21:24:17

If you want to access a value in an indexed, associative, or multidimensional array, you can do it by using the index or the key.
 

Example 1: How to get a value from an array
 

<?php
 $numbers = array(1, 2, 3, 4, 5);
 echo $numbers[0];
?>


Output:

1

Example 2: How to get a value from an associative array


<?php
 $languages = array("p"=>"PHP", "j"=>"Java", "a"=>"Ada", "h"=>"HTML");

 echo $languages["p"];
?>


Output:

PHP
 

Example 3: How to get a value from a multidimensional array


<?php
 $school = array(
   array(
     "name" => "Frédéric Majory",
     "email" => "FredericMajory@dayrep.com",
     "telephone" => "+1 01.09.94.30.12",
     "address" => "94, street Gustave Eiffel",
   ),
   array(
     "name" => "Adorlee Miron",
     "email" => "AdorleeMiron@teleworm.fr",
     "telephone" => "+1 04.97.35.65.26",
     "address" => "32, boulevard Aristide Briand",
   ),
   array(
     "name" => "Christian Leclerc",
     "email" => "ChristianLeclerc@dayrep.com",
     "telephone" => "+1 03.56.16.29.48",
     "address" => "16, street Belges",
   )
 );

 echo $school[0]["name"]. '<br />';  
 echo $school[1]["email"]. '<br />';
?>


Output:

Frédéric Majory
AdorleeMiron@teleworm.fr

About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.