douju8782 2018-10-15 07:08
浏览 69

如何基于整数值将数组拆分为两个数组

Here is an example array I want to split:

(1428,217,1428)

How do I split it in 2 array like this?

(1428,1428)
(217)

I have tried following way but it's only return 1428 array.

$counts = array_count_values($array);
$filtered = array_filter($array, function ($value) use ($counts) {
    return $counts[$value] > 1;

});
  • 写回答

3条回答 默认 最新

  • douwa0280 2018-10-15 07:37
    关注

    One way to solve this for your example data is to sort the array and use array_shift to get the first element of the array and store that in an array.

    $a = [1428,217,1428];
    sort($a);
    $b = [array_shift($a)];
    
    print_r($a);
    print_r($b);
    

    Result

    Array
    (
        [0] => 1428
        [1] => 1428
    )
    Array
    (
        [0] => 217
    )
    
    评论

报告相同问题?