如何使用自定义或用户定义的标准在php中合并和排序数组?

如何使用自定义或用户定义的标准在php中合并和排序数组?

问题描述:

I have two array :

$data1 = array( 
        (0) => array("level" => 1, "id" => 1, "index" => 1, "amount" => 50000),
        (1) => array("level" => 1, "id" => 2, "index" => 1, "amount" => 40000),
        (2) => array("level" => 1, "id" => 3, "index" => 1, "amount" => 0));  

$data2 = array( 
        (0) => array("level" => 2, "id" => 1, "index" => 1, "amount" => 30000),
        (1) => array("level" => 2, "id" => 1, "index" => 2, "amount" => 20000),
        (2) => array("level" => 2, "id" => 2, "index" => 1, "amount" => 15000),
        (3) => array("level" => 2, "id" => 2, "index" => 2, "amount" => 25000),
        (4) => array("level" => 2, "id" => 3, "index" => 1, "amount" => 0));

I want to merge those array into one array and the result is like this :

$expected = array(
         (0) => array("level" => 1, "id" => 1, "index" => 1, "amount" => 50000),
         (1) => array("level" => 2, "id" => 1, "index" => 1, "amount" => 30000),
         (2) => array("level" => 2, "id" => 1, "index" => 2, "amount" => 20000),
         (3) => array("level" => 1, "id" => 2, "index" => 1, "amount" => 40000),
         (4) => array("level" => 2, "id" => 2, "index" => 1, "amount" => 15000),
         (5) => array("level" => 2, "id" => 2, "index" => 2, "amount" => 25000));

I've tried to use array_merge_recursive :

$try = array_merge_recursive($data1, $data2);  

But the result is like this :

$try = array(
         (0) => array("level" => 1, "id" => 1, "index" => 1, "amount" => 50000),
         (1) => array("level" => 1, "id" => 2, "index" => 1, "amount" => 40000),
         (3) => array("level" => 1, "id" => 3, "index" => 1, "amount" => 0),
         (4) => array("level" => 2, "id" => 1, "index" => 1, "amount" => 30000),
         (5) => array("level" => 2, "id" => 1, "index" => 2, "amount" => 20000),             
         (6) => array("level" => 2, "id" => 2, "index" => 1, "amount" => 15000),
         (7) => array("level" => 2, "id" => 2, "index" => 2, "amount" => 25000),
         (8) => array("level" => 2, "id" => 3, "index" => 1, "amount" => 0));

I have criteria for my result :

  1. Remove array where amount=0
  2. Sort and order by id,level,index

I've read about uasort and usort but i dont have any idea how to implement that function to match what i need. Is that function (uasort/usort) can solve it or any other idea? Please help me, thanks!

$data = array_merge($data1, $data2);
$data = array_filter($data, function ($d) {
    return $d['amount'] != 0;
});
usort($data, function ($a, $b) {
    if ($a['id'] != $b['id'])       return $a['id'] - $b['id'];
    if ($a['level'] != $b['level']) return $a['level'] - $b['level'];
    else                            return $a['index'] - $b['index'];
});

Note: Uses PHP 5.3+ anonymous function syntax.

Does this work for you:

$finalArr = array_merge($data1,$data2);

foreach($finalArr as $key => $val) {
    if(empty($val['amount'])) {
        unset($finalArr[$key]);
    }
}
echo "<pre>";
sort($finalArr);
print_r($finalArr);

Hope it helps