How to flatten array in PHP? How to flatten array in PHP? arrays arrays

How to flatten array in PHP?


In PHP 5.5 you have array_column:

$plucked = array_column($yourArray, 'email');

Otherwise, go with array_map:

$plucked = array_map(function($item){ return $item['email'];}, $yourArray);


You can use a RecursiveArrayIterator . This can flatten up even multi-nested arrays.

<?php$arr1=array(0=> array("email"=>"test01@testmail.com"),1=>array("email"=>"test02@testmail.com"),2=> array("email"=>"test03@testmail.com"),    3=>array("email"=>"test04@testmail.com"));echo "<pre>";$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr1));$new_arr = array();foreach($iter as $v) {    $new_arr[]=$v;}print_r($new_arr);

OUTPUT:

Array(    [0] => test01@testmail.com    [1] => test02@testmail.com    [2] => test03@testmail.com    [3] => test04@testmail.com)