jhchen
jhchen

Reputation: 14757

Converting php array of arrays into single array

I have an array whose values are all arrays of a specific format that looks like this:

Array
(
    [0] => Array
           (
               [username] => John
           )    
    [1] => Array
           (
               [username] => Joe
           )
    [2] => Array
           (
               [username] => Jake
           )
)

and I would like to have this:

Array
(
    [0] => John   
    [1] => Joe
    [2] => Jake
)

I can do this manually with a loop but is there a better way? If not, is it possible to do this for an array of objects that have a common attribute?

Upvotes: 39

Views: 52823

Answers (11)

xpoback
xpoback

Reputation: 275

Since PHP 5.6 you can use the splat operator. In case array keys are numeric, you could use this:

$newArray = array_merge(...$oldArray);

Upvotes: 12

vfsoraki
vfsoraki

Reputation: 2307

For other readers, like myself, if the key username is different for each internal array, and you want to flatten it, use this:

call_user_func_array('array_merge', $the_array);

Note that the keys have to be different. Like this:

[
 0 => [
  'a' => 1
 ],
 1 => [
  'b' => 2
 ]
]

After running the code, you have this:

[
 'a' => 1,
 'b' => 2,
]

Upvotes: 5

smarber
smarber

Reputation: 5074

For those who're using composer:

  1. if <=php5.5 array_column can be used
  2. if >php5.5 available polyfills such as https://github.com/symfony/polyfill/tree/master/src/Php55 can be used. Once you composer install symfony/polyfill-php55, then you can just call array_column as if your php5.4 or below had it.

Upvotes: 0

Manoj Thapliyal
Manoj Thapliyal

Reputation: 577

$new_array = array_column($last_array, 'username');

Upvotes: 6

MrUpsidown
MrUpsidown

Reputation: 22490

If you don't care about the keys:

$new_array = array();

foreach($array as $v) {

    $new_array[] = $v['username']
}

Upvotes: 0

Nanhe Kumar
Nanhe Kumar

Reputation: 16297

Try this:

$list=array();
foreach($array as $v) {
    array_push($list, $v['username']);
}

Upvotes: 0

Peter Taylor
Peter Taylor

Reputation: 5036

Since PHP 5.5.0 there is a built-in function array_column which does exactly this.

Upvotes: 45

Pavunkumar
Pavunkumar

Reputation: 5335

Try this code:

foreach   ($arr as $key => $val)
{
    sort($val );
    $new = array_merge($val, $new);
}
print_r ($new);

Upvotes: 0

thetaiko
thetaiko

Reputation: 7824

$new = array_map(create_function('$auser', 'return $auser["username"];'), $array);

If using objects you could just change return $auser["username"]; to return $auser->get_username();

Upvotes: 0

user32117
user32117

Reputation:

If you're using PHP 5.3 you can make use of array_walk_recursive and a closure (the closure can be replaced by a normal function if your PHP version < 5.3):

function arrayFlatten(array $array) {
    $flatten = array();
    array_walk_recursive($array, function($value) use(&$flatten) {
        $flatten[] = $value;
    });

    return $flatten;
}

$nested = array(
    array('username' => 'John'),
    array('username' => 'Joe'),
    array('username' => 'Jake'),
);

$flattened = arrayFlatten($nested);

var_dump($flattened)

array
  0 => string 'John' (length=4)
  1 => string 'Joe' (length=3)
  2 => string 'Jake' (length=4)

Upvotes: 2

SilentGhost
SilentGhost

Reputation: 319551

why complicate things?

foreach($array as $k=>$v) {
    $new[$k] = $v['username'];
}

Upvotes: 33

Related Questions