How to Get Single Value from Array in PHP?

You will be told through this article how you can get a single value from array via php. So let’s try to understand.

Extract Single Value from array in PHP, Get Single Value from Index Array in PHP, Get Single Value from Associative Array Using PHP, Get Single Value from Multidimensional Array Using PHP

First of all I would like to tell you that arrays are of 3 types. I will show you how to extract a single value from these 3 types of arrays. The 3 arrays are as follows.

  1. Indexed array.
  2. Associative array.
  3. Multidimensional array.

Get Single Value from Indexed Array in PHP

1.Indexed array.

How you can get single value through index array. Example of this is given below.

Example :-

<?php 
$colors = array("red", "pink", "black", "yellow", "green");
echo $colors[0]; // Outputs: red
echo "<br>";
echo $colors[1]; // Outputs: pink
echo "<br>";
?>

Get Single Value from Associative Array in PHP

2.Associative array.

How you can get single value through associative array. Example of this is given below.

Example :-

<?php 
$colors = array("color"=>"Red", "color1"=>"yellow", "color2"=>"pink", "color3"=>"orange");
echo $colors["color"]; // Outputs: Red
echo "<br>";
echo $colors["color3"]; // Outputs: orange
echo "<br>";
?>

Get Single Value from Multidimensional Array in PHP

3.Multidimensional array.

How you can get single value through Multidimensional array. Example of this is given below.

Example :-

<?php 
$users = array(
array(
"name" => "Jyoti",
"age" => "25",
),
array(
"name" => "Iron Man",
"age" => "55",
),
array(
"name" => "Ponting",
"age" => "41",
)
);
echo $users[0]["name"]; // Outputs: Jyoti
echo "<br>";
echo $users[1]["age"]; // Outputs: 55
?>