从多维数组中选择具有相同contestId的2个项目

时间:2013-05-09 19:54:15

标签: php arrays codeigniter

我想从共享相同比赛ID的这一系列视频中提取2个视频。无论比赛ID是什么,只要两个项目共享相同的值。这可能很容易,但我正处于我的大脑被炸的那一点。我能闻到烧烤的味道。洛尔。

    Array
(
    [0] => Array
        (
            [id] => 11
            [filename] => 7966727971960279-4131736221448300-Adele_Set_Fire_to_the_Rain_Cover_by_Sara_Niemietz_
            [added] => 2013-05-06 08:50:34
            [userId] => 14
            [contestId] => 3
            [complete] => 1
        )

    [1] => Array
        (
            [id] => 15
            [filename] => 7090052934711575-3964279645504194-Adele_Set_Fire_To_The_Rain_cover_by_Sabrina
            [added] => 2013-05-06 09:58:08
            [userId] => 14
            [contestId] => 4
            [complete] => 1
        )

    [2] => Array
        (
            [id] => 14
            [filename] => 6959153088629946-5656902896285110-Adele_Set_Fire_to_the_Rain_Cover_By_Public_School_Kids_Own_With_Guitar
            [added] => 2013-05-06 09:42:10
            [userId] => 14
            [contestId] => 4
            [complete] => 1
        )

    [3] => Array
        (
            [id] => 10
            [filename] => 2795880037180269-1520875971490576-test
            [added] => 2013-05-06 08:02:53
            [userId] => 9
            [contestId] => 3
            [complete] => 1
        )

    [4] => Array
        (
            [id] => 12
            [filename] => 6683752765687164-9656171421456902-Adele_Set_Fire_To_The_Rain_Arlene_Zelina_Cover_
            [added] => 2013-05-06 09:21:12
            [userId] => 11
            [contestId] => 3
            [complete] => 1
        )

    [5] => Array
        (
            [id] => 13
            [filename] => 6917133659214044-8729839283435145-Adele_Set_Fire_To_The_Rain_Boyce_Avenue_cover_on_iTunes_Spotify
            [added] => 2013-05-06 09:34:50
            [userId] => 12
            [contestId] => 3
            [complete] => 1
        )

)

2 个答案:

答案 0 :(得分:2)

您可以将它们存储在一个数组中,每个元素都是一个子数组,所有元素都共享contestId

$sorted = array();

foreach ($elements as $e) {
    $sorted[$e['contestId']][] = $e;
}

print_r($sorted);

修改

此函数将为您提供一个包含两个具有相同contestId的元素的数组,如果未找到则返回null。如果您不需要它们是随机的,请删除shuffle()来电。

function getElementsFromArray($elements, $numberOfElements = 2) {
    shuffle($elements); // In case if you want different elements in each call
    $sorted = array();

    foreach ($elements as $e) {
        $contestId = $e['contestId'];
        $sorted[$contestId][] = $e;

        if (count($sorted[$contestId]) == $numberOfElements)
            return $sorted[$contestId];
    }

    return null;        
}

答案 1 :(得分:1)

这里你去:这将返回你发现重复的第一个比赛ID的前两个视频。如果您需要两个以上或者您需要所有重复的ID,我认为您可以根据您的要求修改此模板。

<?

$vidArr = array();
foreach($arr as $ar)
{
    if(!array_key_exists($ar['contestId'],$vidArr)) 
    {
        $vidArr[$ar['contestId']] = $ar['filename'];    
    }
    else
    {
        echo "Found two videos '". $vidArr[$ar['contestId']] ."' and '".$ar['filename']."' for ContestID ".$ar['contestId'];
        exit;
    }   
}