php

How to get a value from an array in PHP

In this tutorial, we are going to see how to get a value from an array in PHP. 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" => "[email protected]",
		  "telephone" => "+1 01.09.94.30.12",
		  "address" => "94, street Gustave Eiffel",
		),
		array(
		  "name" => "Adorlee Miron",
		  "email" => "[email protected]",
		  "telephone" => "+1 04.97.35.65.26",
		  "address" => "32, boulevard Aristide Briand",
		),
		array(
		  "name" => "Christian Leclerc",
		  "email" => "[email protected]",
		  "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
[email protected]
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *