简体   繁体   中英

multi-dimensional array into a single layered array PHP

I have the following array being returned

Array
    (
        [0] => Array
            (
                [uid] => 616941445
            )

        [1] => Array
            (
                [uid] => 1354124203
            )

    )

However I want just a single layered array, so i would like something like this.

Array
(     
[0] => 616941445
[1] => 1354124203
)
foreach ($arr as $key => $val) {
  $arr[$key] = $val['uid'];
}
<?php
$multi_arr = array(
    array(
        'uid' => 616941445
    ),
    array(
        'uid' => 1354124203
    ),
);

$single_arr = array();
foreach($multi_arr as $arr){
    foreach($arr as $val) $single_arr[] = $val;
}
?>
foreach($arr as $key=>$val) {
    $single_arr[] = $arr[$key]['uid'];
}

As always, when you need to change two level array into one level without preserve keys:

$your2DArray = array(/* .. */);
$flatArray = array_map('array_pop', $your2DArray);

And like you want to, no loops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM