I have a array with values based on a specific player and I want to use some of these values(match_id and minutes) to compare to a second array and then create the third array to hold the returned values.
I'm writing this for a Joomla based website and so I'm using an inbuilt array functions LoadRowList(); which returns an indexed array of indexed arrays from the table records returned by the query.
The first array contains values populated based on a particular player
$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();
The $search array outputs an array in the format below. print_r($search);
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 )
)
The second array contains hundreds of records with the same layout as the first.
$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();
The $data array outputs in the same format as above. print_r($data);
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
And finally, I need the third array to be populated by records that are a match between the $search and $data arrays based only on match_id and minutes.
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
I have revised my original question to include more information I'm no expert so I'm looking for all and any ideas and feedback. Thanks
Note:The column names areas follows Name = first_name, Match ID = match_id, Match name = m_name,The event time = minutes and the Points scored = points
Issues I'm having In response to comments this is my problem take the following code for example (its only an example):
$matches = array();
foreach ($data as $entry){
if ($entry[2] == $search[2]) {
match =$search;
$matches[] = $match
}
}
print_r($mtaches)
If I use a comparison like $entry[2] == $search[2] it will be comparing the 2nd arrays from each ie
`[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 )`
so my question is how do you use a type of wildcard so I can compare key values ie
$entry[*][2] == $search[*][2]
Thanks to everyone for their help and patience and especially to @erisco for sharing his knowledg开发者_StackOverflow中文版e
This problem can likely be solved by a change in query, but without an exact database structure given it is difficult to guess as to what it may look like. If I couldn't change the query, I would solve the problem along these lines.
You want to match based on match_id and minutes, so it would be ideal to have those as indexes.
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 )
)
Changes to:
array(
33 => array(
7 => array("Marco", "Xornada 04", 3)
),
129 => array(
1 => array("Marco", "Xornada 08", 2)
6 => array("Marco", "Xornada 08", 2)
)
)
The same would apply to the second array as well. Then, you can merge them together based on key. If your second array looked like this:
array(
33 => array(
7 => array("Pablo", "Xornada 04", 3)
)
)
Then the merged version would look like this:
array(
33 => array(
7 => array(
array("Marco", "Xornada 04", 3),
array("Pablo", "Xornada 04", 3)
)
)
)
(Whether you throw out non-matches at the time of merging or if you simply exclude match_id/minutes indexes that don't have at least two players later on is up to you.)
Now you can reduce this array to your desired form.
array(
array("Marco", 33, "Xornada 04", 7, 3, 3, "Pablo")
)
This is an example of how the transformation could take place.
<?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);
$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);
精彩评论