使用条件逻辑过滤多维数组

时间:2016-05-29 11:33:29

标签: php arrays array-filter

我有以下数组,

Array ( [0] => Array ( [location] => X33 [usernumber] => 1 [order] => XX [part_number] => Hi ) [1] => Array ( [location] => X33 [usernumber] => 1 [order] => XX [part_number] => 68730 ) [2] => Array ( [location] => W33 [usernumber] => 2 [order] => YY [part_number] => 68741) [3] => Array ( [location] => W33 [usernumber] => 2 [order] => YY [part_number] => Hello )

我想用usernumber = 1过滤这个数组,这样就会创建1个数组,其数组的usernumber = 1,同样也会为usernumber = 2创建

我在数据库中有用户并将在此数组中搜索用户

我尝试了下面的代码,

$users = $this->admin_model->get_usersforshipment();
foreach ($users as $user) {

  $filtered = array_filter($csv_array, function($user)

   { //Below is retrurning as orignal $csv_array, not filtered,
    return !empty($user['usernumber']); 

  });

}

当$ users [' usernumber] == 1

时所需的输出
Array ( [0] => Array ( [location] => X33 [usernumber] => 1 [order] => XX [part_number] => Hi ) [1] => Array ( [location] => X33 [usernumber] => 1 [order] => XX [part_number] => 68730 ) )

当$ users [' usernumber] == 2

时所需的输出
Array ( [0] => Array ( [location] => W33 [usernumber] => 2 [order] => YY [part_number] => 68741) [1] => Array ( [location] => W33 [usernumber] => 2 [order] => YY [part_number] => Hello ) 

如何从Multi Dimension数组中仅过滤2个数组?

2 个答案:

答案 0 :(得分:1)

目前,您的数组定义如下:

$array = [
    0 => [
        'location' => l1
        'usernumber' => 1
        'order' => 'o1'
    ],
    1 => [
        'location' => l2
        'usernumber' => 1
        'order' => 'o2'
    ],
    2 => [
        'location' => l3
        'usernumber' => 2
        'order' => 'o3'
    ]
];

一个好的解决方案是将usernumber变量设置为数组键。您可以在创建阵列时执行此操作,也可以在创建后更改它。它应该是这样的:

$array = [
    1 => [ // The key is now the usernumber
        [
            'location' => 'l1'
            'order' => 'o1'
        ],
        [
            'location' => 'l2'
            'order' => 'o2'
        ]
    ],
    2 => [
        [
            'location' => 'l3'
            'order' => 'o3'
        ],
    ]
];

现在您可以通过用户编号轻松获取不同的订单并循环显示它们:

$orders = $array[1]; // Get all orders from the user with usernumber 1

foreach ($orders as $order) {
    print_r($order);
}

答案 1 :(得分:1)

Online Example,在您的反馈后添加说明。

$arr = array(
        array ('location' => 'X33',
               'usernumber' => 1,
               'order' => 'XX', 
               'part_number' => 'Hi'
              ),
        array ('location' => 'X33',
               'usernumber' => 1,
               'order' => 'XX',
               'part_number' => '68730' 
               ),
        array ('location' => 'W33',
               'usernumber' => 2,
               'order' => 'YY',
               'part_number' => '68741'
               ),
        array ('location' => 'W33',
               'usernumber' => 2,
               'order' => 'YY',
               'part_number' => 'Hello'
              )
    );

$out = array();
$index = $arr[0]['usernumber'];
foreach($arr as $val){
    if($index != $val['usernumber'])
        $index = $val['usernumber'];
    $out[$index][] = $val;
}
echo '<pre>';
print_r($out);