PHP,比较两个数组然后用结果创建第三个数组

时间:2011-02-01 10:33:57

标签: php arrays compare

我有一个基于特定播放器的值的数组,我想使用其中一些值(match_id和minutes)来比较第二个数组,然后创建第三个数组来保存返回的值。

我正在为基于Joomla的网站编写这个,所以我使用的是内置数组函数LoadRowList();它从查询返回的表记录中返回索引数组的索引数组。

第一个数组包含基于特定播放器填充的值

$search = array();
                $query = "SELECT first_name,match_id,m_name,minutes,points
                          FROM #__bl_players as pl,#__bl_match_events as me,#__bl_matchday as md, #__bl_match as m
                          WHERE pl.id = me.player_id
                          AND pl.id = ".$player_id."
                          AND me.match_id = m.id
                          AND m.m_id = md.id
                          ";
        $db->setQuery($query);
        $search = $db->loadRowList();

$ search数组以下面的格式输出数组。的print_r($搜索);

Array ( 
        [0] => Array ( [0] => Marco [1] => 33 [2] => Xornada 04 [3] => 7 [4] => 3 ) 
        [1] => Array ( [0] => Marco [1] => 129 [2] => Xornada 08 [3] => 1 [4] => 2 ) 
        [2] => Array ( [0] => Marco [1] => 129 [2] => Xornada 08 [3] => 6 [4] => 2 ) 
        )

第二个数组包含数百条与第一个相同布局的记录。

$data = array();
                $query = "SELECT first_name,match_id, m_name,minutes,ecount
                FROM #__bl_players as pl,#__bl_match_events as me,#__bl_matchday as md, #__bl_match as m
                WHERE pl.id = me.player_id
                AND me.match_id = m.id
                AND m.m_id = md.id
                AND md.s_id = ".$s_id."
                         ";
        $db->setQuery($query);
        $data = $db->loadRowList();

$ data数组以与上面相同的格式输出。的print_r($数据);

Array ( 
        [0] => Array ( [0] => Pablo [1] => 8 [2] => Xornada 01 [3] => 2 [4] => 3 ) 
        [1] => Array ( [0] => Juan Ramón [1] => 9 [2] => Xornada 01 [3] => 1 [4] => 0 ) 
        [2] => Array ( [0] => Pedro [1] => 9 [2] => Xornada 01 [3] => 1 [4] => 3 )
)etc etc etc 

最后,我需要第三个数组由仅基于match_id和分钟的$ search和$ data数组匹配的记录填充。

    Array(
    [0]=>Array ([1]=> PlayerName(from the first array)[2]=> MatchID[3]=> MatchName[4]=> TimePlayed[5]=> Score(from the first array)[6]=> Score(from the second array)[7]=> PlayerName(from the second array))
)etc etc etc

我修改了我的原始问题以包含更多信息我不是专家所以我正在寻找所有的想法和反馈。感谢

注意:列名称区域遵循Name = first_name,匹配ID = match_id,匹配名称= m_name,事件时间=分钟,点数得分=点数

我遇到的问题 回答评论这是我的问题,以下面的代码为例(它只是一个例子):

$matches = array();
foreach ($data as $entry){
    if ($entry[2] == $search[2]) {
match =$search;
$matches[] = $match
    }
}
print_r($mtaches)

如果我使用像$ entry [2] == $ search [2]这样的比较,它将比较每个的第二个数组,即

`[2] => Array ( [0] => Marco [1] => 129 [2] => Xornada 08 [3] => 6 [4] => 2 ) 

==

[2] => Array ( [0] => Pedro [1] => 9 [2] => Xornada 01 [3] => 1 [4] => 3 )`

所以我的问题是你如何使用一种通配符,所以我可以比较关键值,即

$entry[*][2] == $search[*][2]

感谢大家的帮助和耐心,尤其感谢@erisco分享他的知识

2 个答案:

答案 0 :(得分:4)

这个问题很可能通过查询更改来解决,但是如果没有确切的数据库结构,很难猜测它看起来像什么。如果我无法更改查询,我会沿着这些方向解决问题。

您希望根据match_id和分钟进行匹配,因此将这些作为索引是理想的。

Array ( 
        [0] => Array ( [0] => Marco [1] => 33 [2] => Xornada 04 [3] => 7 [4] => 3 ) 
        [1] => Array ( [0] => Marco [1] => 129 [2] => Xornada 08 [3] => 1 [4] => 2 ) 
        [2] => Array ( [0] => Marco [1] => 129 [2] => Xornada 08 [3] => 6 [4] => 2 ) 
        )

更改为:

array(
    33 => array(
        7 => array("Marco", "Xornada 04", 3)
    ),
    129 => array(
        1 => array("Marco", "Xornada 08", 2)
        6 => array("Marco", "Xornada 08", 2)
    )
)

同样适用于第二个数组。然后,您可以根据键将它们合并在一起。如果您的第二个阵列看起来像这样:

array(
    33 => array(
        7 => array("Pablo", "Xornada 04", 3)
    )
)

然后合并的版本将如下所示:

array(
    33 => array(
        7 => array(
            array("Marco", "Xornada 04", 3),
            array("Pablo", "Xornada 04", 3)
        )
    )   
)

(无论你在合并时是否抛弃不匹配,或者如果你只是排除之后至少没有两个玩家的match_id / minutes索引,由你决定。)

现在您可以将此数组缩减为所需的格式。

array(
    array("Marco", 33, "Xornada 04", 7, 3, 3, "Pablo")
)

这是如何进行转型的一个例子。

<?php

$one = array( 
    array("Marco", 33, "Xornada 04", 7, 3), 
    array("Marco", 129, "Xornada 08", 1, 2), 
    array("Marco", 129, "Xornada 08", 6, 2)
);

$two = array(
    array("Pablo", 33, "Xornada 04", 7, 1), 
    array("Pedro", 129, "Xornada 08", 1, 0), 
    array("Garcia", 45, "Xornada 01", 2, 2)
);

$three = array();
foreach ($one as $o) {
    $three[$o[1]][$o[3]][] = array($o[0], $o[2], $o[4]);
}

foreach ($two as $t) {
    if (isset($three[$t[1]][$t[3]])) {
        $three[$t[1]][$t[3]][] = array($t[0], $t[2], $t[4]);
    }
}

$four = array();
foreach ($three as $matchId => $t) {
    foreach ($t as $minutes => $tt) {
        // filter out non-matches
        if (count($tt) > 1) {
            $p1 = $tt[0];
            $p2 = $tt[1];
            $four[] = array($p1[0], $matchId, $p1[1], $minutes, $p1[2], $p2[2], $p2[0]);
        }
    }
}

print_r($four);

答案 1 :(得分:-1)

$search = array(
    1 => 'PlayerName',
    2 => 'MatchID',
    3 => 'MatchName',
    4 => 'TimePlayed',
    5 => 'Score',
);

$data = array(
    array(
        // match
        1 => 'PlayerName',
        2 => 'MatchID',
        3 => 'MatchName',
        4 => 'TimePlayed',
        5 => 'Score',
    ),
    array(
        // match
        1 => 'OtherPlayerName',
        2 => 'MatchID',
        3 => 'OtherMatchName',
        4 => 'TimePlayed',
        5 => 'OtherScore',
    ),
    array(
        // no match
        1 => 'OtherPlayerName',
        2 => 'OtherMatchID',
        3 => 'OtherMatchName',
        4 => 'OtherTimePlayed',
        5 => 'OtherScore',
    ),  
);

$matches = array();
foreach ($data as $entry) {
    if ($entry[2] == $search[2] && $entry[4] == $search[4]) {
        $match = $search;
        $match[6] = $entry[5];
        $match[7] = $entry[1];
        $matches[] = $match;
    }
}

print_r($matches);