多维数组成PHP单层数组

时间:2021-05-18 21:30:58

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
)

4 个解决方案

#1


4  

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

#2


0  

<?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;
}
?>

#3


0  

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.

就像你想要的那样,没有循环。

#4


0  

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

#1


4  

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

#2


0  

<?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;
}
?>

#3


0  

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.

就像你想要的那样,没有循环。

#4


0  

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