仅保留二维数组每行的最后两个元素

问题描述 投票:0回答:2

我有一个 PHP 数组

Array
(
    [0] => Array(
        [0] => 24
        [1] => 1
        [2] => 0
        [3] => 4
    )
    [1] => Array(
        [0] => 2
        [1] => 23
        [2] => 5
        [3] => 0
    )
    [2] => Array(
        [0] => 22
        [1] => 3
        [2] => 0
        [3] => 6
    )
)         

对于列 [2][3],我想从上面的数组创建一个新数组,应如下所示。

Array
(
    [0] => Array(
        [0] => 0
        [1] => 4
    )
    [1] => Array(
        [0] => 5
        [1] => 0
    )
    [2] => Array(
        [0] => 0
        [1] => 6
    )
)
php arrays sanitization
2个回答
9
投票
array_map(function($elem) { 
  return array($elem[2], $elem[3]); 
}, $array);

5
投票

试试这个 -

$newArray = array();
foreach ($yourArray as $value) {
    $newArray[] = array($value[2], $value[3]);
}
© www.soinside.com 2019 - 2024. All rights reserved.