按键值将数组拆分为两个相同的数组

时间:2013-03-03 21:14:03

标签: php arrays

我有一个如下所示的数组:

MyArray (
    [0] => Array (
                            [board_id] => 1047999
                            [added_date] => 2013-02-28 11:14:02
                            [type] => 4
                           )
    [1] => Array (
                           [board_id] => 1047999
                           [added_date] => 2013-02-28 11:14:02
                           [type] => 1
                          )
    [2] => Array (
                          [board_id] => 1047999
                          [added_date] => 2013-02-28 11:14:02
                          [type] => 4
                          )
    [3] => Array (
                          [board_id] => 1047999
                          [added_date] => 2013-02-28 11:14:02
                          [type] => 1
                          )
)

如何通过type拆分此数组并保持完全相同的数组结构?即我有MyArrayT4有两个子数组,MyArrayT1有两个子数组吗?

TIA!

1 个答案:

答案 0 :(得分:2)

像这样使用array_filter 编辑使用array_values重置

$filtering = function($type) use($your_array) {
    $filtered = array_filter($your_array, function($r) use($type) {
        return $r['type'] == $type;
    });

    return array_values($filtered);
};


$array_1 = $filtering->__invoke('4');
$array_2 = $filtering->__invoke('1');