元素被洗牌时的Array_map

时间:2015-05-25 15:26:29

标签: php shuffle array-map

我有这个数组(它只是其中的一部分)。 6 =问题ID,optionIDs =可能的答案。

Array
 (
    [3] => Array
        (
        [0] => 6
        [1] => Array
            (
                [0] => Array
                    (
                        [optionID] => 16
                        [isCorrect] => 0
                    )

                [1] => Array
                    (
                        [optionID] => 14
                        [isCorrect] => 1
                    )

                [2] => Array
                    (
                        [optionID] => 15
                        [isCorrect] => 0
                    )

                [3] => Array
                    (
                        [optionID] => 17
                        [isCorrect] => 0
                    )

            )

    )

[7] => Array
    (
        [0] => 6
        [1] => Array
            (
                [0] => Array
                    (
                        [optionID] => 16
                        [isCorrect] => 0
                    )

                [1] => Array
                    (
                        [optionID] => 15
                        [isCorrect] => 0
                    )

                [2] => Array
                    (
                        [optionID] => 17
                        [isCorrect] => 0
                    )

                [3] => Array
                    (
                        [optionID] => 14
                        [isCorrect] => 1
                    )

            )

    )

)

我试图将冗余问题(6和6)与array_map合并:

    $unique = array_map('unserialize', array_unique(array_map('serialize', $quizQuestionArray)));

只要optionID的顺序相同,它就可以正常工作。但在某些情况下(比如这里),他们被改组(16,14,15,17)(16,15,17,14)。有没有办法让他们洗牌并删除重复的问题?

2 个答案:

答案 0 :(得分:2)

array_map-serialize是一种重复删除数组的粗略方法。你应该使用这样的东西:

$dupeIds = [];
$array = array_filter($array, function ($item) use (&$dupeIds) {
    $keep = !isset($dupeIds[$item[0]]);
    $dupeIds[$item[0]] = true;
    return $keep;
});

答案 1 :(得分:1)

在应用array_map()函数之前,您需要将它们排序为相同的顺序。您可以使用uasort()函数并提供您自己的比较函数,如下所示:

// Example array
$array = array(
    3 => array(
        0 => 6,
        1 => array(
            0 => array(
                'optionID' => 16,
                'isCorrect' => 0
            ),
            1 => array(
                'optionID' => 14,
                'isCorrect' => 1
            ),
            2 => array(
                'optionID' => 15,
                'isCorrect' => 0
            ),
            3 => array(
                'optionID' => 17,
                'isCorrect' => 0
            ),
        )
    ),
    7 => array(
        0 => 6,
        1 => array(
            0 => array(
                'optionID' => 16,
                'isCorrect' => 0
            ),
            1 => array(
                'optionID' => 15,
                'isCorrect' => 0
            ),
            2 => array(
                'optionID' => 17,
                'isCorrect' => 0
            ),
            3 => array(
                'optionID' => 14,
                'isCorrect' => 1
            ),
        )
    )
);

// You can supply parts of an array to uasort()
// uasort() will modify your array but keep your keys.
uasort($array[3][2], 'sort_by_optionid');
uasort($array[7][3], 'sort_by_optionid');

function sort_by_optionid($a, $b) {
    if ($a['optionID'] === $b['optionID']) {
        return 0;
    } else if ($a['optionID'] > $b['optionID']) {
        return 1;
    } else {
        return -1;
    }
}
// Done. 

现在保存了密钥,您可以轻松地使用array_map()查找重复项,然后根据密钥再次排序回原始状态。例如。 uksort()