ItSolutionStuff.com

How to Get Specific Key Value from Multidimensional Array PHP?

By Hardik Savani • May 14, 2024
PHP

In this post, i will learn you how to get specific key value array from multidimensional array in php. we will get specific key value array using array_column() and array_map().

we almost require to get specific key and value in array when work with php multidimensional array. like if you have one multidimensional array with each array with id, name, email etc key. You need to get only all name from array then how you can get it?, i will show you how we can do it. i will give you full example:

$names = array_column($myArray, 'name');

$emails = array_map(function ($ar) {return $ar['email'];}, $myArray);

Example:

<?php

$myArray = [

[

'name' => 'Paresh',

'email' => 'paresh@gmail.com'

],

[

'name' => 'Rakesh',

'email' => 'rakesh@gmail.com'

],

[

'name' => 'Naresh',

'email' => 'naresh@gmail.com'

],

];

$names = array_column($myArray, 'name');

$emails = array_map(function ($ar) {return $ar['email'];}, $myArray);

print_r($names);

print_r($emails);

?>

Output:

Array

(

[0] => Paresh

[1] => Rakesh

[2] => Naresh

)

Array

(

[0] => paresh@gmail.com

[1] => rakesh@gmail.com

[2] => naresh@gmail.com

)

I hope it can help you....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Remove First Element from Array in PHP?

Read Now →

PHP Multidimensional Array Search By Value Example

Read Now →

PHP Check If Array Is Multidimensional or Not

Read Now →

How to Remove Multiple Keys from PHP Array?

Read Now →

PHP - How to Reindex Array Key After Unset Key?

Read Now →

AngularJS Convert Comma Separated String to Array Example

Read Now →

PHP Remove Duplicates from Multidimensional Array Example

Read Now →

How to Get Maximum Key Value of Array in PHP?

Read Now →

How to Remove Empty Values from Array in PHP?

Read Now →

How to Add Prefix in Each Key of PHP Array?

Read Now →

How to Get Minimum Key Value of Array in PHP?

Read Now →

How to Remove Null Values from Array in PHP?

Read Now →

How to Merge Two Array with Same Keys without Loop in PHP?

Read Now →