How to get specific key value from multidimensional array in php

Admin   PHP   49  2020-08-12 00:29:21

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' => '[email protected]'

],

[

'name' => 'Rakesh',

'email' => '[email protected]'

],

[

'name' => 'Naresh',

'email' => '[email protected]'

],

];

$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] => [email protected]

[1] => [email protected]

[2] => [email protected]

)

I hope it can help you....