PHP, compare two arrays then create a third array with the results - php

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 knowledge

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);

Related

Finding max with multiple key filter inside an array

I have script which needs to find the star performer of the month.
This is sample data
$data = array (
[0] = array(
"tasks_done" => 5,
"rating" => 5),
[1] = array(
"tasks_done" => 4,
"rating" => 5),
[2] = array(
"tasks_done" => 3,
"rating" => 5),
[3] = array(
"tasks_done" => 5,
"rating" => 5)
);
So the question is to find the person with max tasks done and max number of rating. Is this possible using any algorithm of by simple php code.
References Sample to find max number in array :
find max() of specific multidimensional array value in php
But nothing seems to work.
Any help is appreciated.
I have sort by rating first and second tasks_done.
Please check below code,
$data = array();
$data[0]=array("tasks_done" => 5,"rating" => 5);
$data[1]=array("tasks_done" => 4,"rating" => 5);
$data[2]=array("tasks_done" => 3,"rating" => 3);
$data[3]=array("tasks_done" => 5,"rating" => 6);
$data[4]=array("tasks_done" => 6,"rating" => 2);
$data[5]=array("tasks_done" => 3,"rating" => 6);
$temp_array = array();
$output = array();
foreach($data as $k=>$values){
$temp_array['tasks_done'][$values['tasks_done']][$k]=$values['tasks_done'];
$temp_array['rating'][$values['rating']][$k]=$values['rating'];
}
echo "<pre>";
print_r($data);
krsort($temp_array['tasks_done']);
krsort($temp_array['rating']);
foreach($temp_array['rating'] as $k=>$v){
$check = checkUpper($v,$temp_array['tasks_done']);
foreach($v as $k1=>$v1){
$output[]=array(
"tasks_done"=>$check[$k1],
"rating"=>$v1
);
}
}
function checkUpper($array_check,$array){
$tmp = array();
foreach($array as $keys=>$values){
foreach($values as $key=>$value){
if(array_key_exists($key,$array_check)){
$tmp[$key]=$value;
}
}
}
arsort($tmp);
return $tmp;
}
Output,
Array
(
[0] => Array
(
[tasks_done] => 5
[rating] => 6
)
[1] => Array
(
[tasks_done] => 3
[rating] => 6
)
[2] => Array
(
[tasks_done] => 5
[rating] => 5
)
[3] => Array
(
[tasks_done] => 4
[rating] => 5
)
[4] => Array
(
[tasks_done] => 3
[rating] => 3
)
[5] => Array
(
[tasks_done] => 6
[rating] => 2
)
)
I solved it using mysql with order by parameter. Thank you folks for your help.

Find maximum value in PHP array

Below is my array:
Array
(
[3] => Array
(
[2] => 3
[4] => 1
[5] => 2
[6] => 2
)
[5] => Array
(
[2] => 1
[3] => 2
[4] => 3
[6] => 3
)
In this array I want to find the maximum number and if the array contains the same maximum values then we choose one maximum value randomly.
Expected output like below:
Array
(
[3] => Array
(
[2] => 3
)
[5] => Array
(
[4] => 3
)
[6] => Array
(
[2] => 3
)
)
This is what I've tried:
$found = Array( [3] => Array ( [2] => 3 [4] => 1 [5] => 2 [6] => 2 ) [5] => Array ( [2] => 1 [3] => 2 [4] => 3 [6] => 3 ) [6] => Array ( [2] => 3 [3] => 2 [4] => 2 [5] => 3 ))
$fmaxnum = array();
foreach($found as $fk => $fv){
$fmaxnum[$fk] = max($fv);
}
echo "<pre>";print_r($fmaxnum);echo "</pre>"
You will get max value with max() but for index you have to use array_keys()
You can try this:
$found = Array ( '3' => Array ( '2' => 3 ,'4' => 1, '5' => 2, '6' => 2 ),
'5' => Array ( '2' => 1 ,'3' => 2, '4' => 3, '6' => 3 ),
'6' => Array ( '2' => 3 ,'3' => 2, '4' => 2, '5' => 3 )
);
$fmaxnum = array();
foreach($found as $fk => $fv){
$max_key = array_keys($fv, max($fv));
$fmaxnum[$fk] = array(
($max_key[0]) => max($fv) /* This will give small index value */
/* (count($max_key)-1) => => max($fv) // this will give highest index value */
);
}
echo "<pre>";
print_r($fmaxnum);
echo "</pre>";
Simple solution with array_map and array_keys functions:
// supposing $arr is your initial array
$arrMax = array_map(function($v){
$maximum = max($v);
return [array_keys($v, $maximum)[0] => $maximum];
}, $arr);
print_r($arrMax);
The output:
Array
(
[3] => Array
(
[2] => 3
)
[5] => Array
(
[4] => 3
)
[6] => Array
(
[2] => 3
)
)
if you just want to know the highest value as I understood
In this array I want to find the maximum number and if the array
contains the same maximum values then we choose one maximum value
randomly.
you could just do this
$array = [['3','1','2','2'],['1','2','3','3'],['2','1','1','3']];
$result = [];
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value) {
$result[] = $value;
}
echo max($result);
this will foreach all the array and push the values on $result, then you just have one level array and easily use max function

add items to multidimensional array programmatically in php

I have the following array
$a = array(0 => 'Item',
1 => 'Wattles',
2 => 'Types',
3 => 'Compost',
4=> 'Estimated',
5 => '123',
6 => 'Actual',
7 => '12',
);
That is sorted with the following code.
echo "<pre>";
print_r($a);
$a_len = count($a);
$fnl = array();
$i = 0;
while($i<$a_len){
$fnl[$a[$i]] = $a[++$i];
$i++;
}
print_r($fnl);
It prints correctly
Array
(
[Item] => Wattles
[Types] => Compost
[Estimated Qty] => 123
[Actual Qty] => 12
)
until i add multiple entries.
Array
(
[0] => Item
[1] => Wattles
[2] => Types
[3] => Compost
[4] => Estimated Qty
[5] => 123
[6] => Actual Qty
[7] => 12
[8] => Item
[9] => Silt Fence
[10] => Types
[11] => Straw
[12] => Estimated Qty
[13] => 45
[14] => Actual Qty
[15] => 142
)
I need to make this add items in a multidimensional array.
$items = array
(
array("Wattles","Silt Fence), //items
array("Compost","Straw"), //types
array(123,45), //estimated quantity
array(12,142) //actual quantity
);
There are a few given numbers. There are exactly 4 entries (8 items) before the list repeats itself.
I have been stuck on this portion for hours, and don't know how to get my code working as I want it to.
To get the expected result with string keys you can do:
foreach(array_chunk($a, 2) as $pairs) {
$result[$pairs[0]][] = $pairs[1];
}
Yields:
Array
(
[Item] => Array
(
[0] => Wattles
[1] => Silt Fence
)
[Types] => Array
(
[0] => Compost
[1] => Straw
)
[Estimated] => Array
(
[0] => 123
[1] => 45
)
[Actual] => Array
(
[0] => 12
[1] => 142
)
)
Then if you want it numerically indexed:
$result = array_values($result);
You have your structure for a multidimensional array wrong. You should construct your array like this:
$a = array(
0 => array(
'Item' => 'Wattles',
'Types' => 'Compost',
'Estimated' => 123,
'Actual' => 12
)
);
Then to add to it:
$a[] = array(
'Item' => 'Silt Fence',
'Types' => 'Straw',
'Estimated' => 45,
'Actual' => 142
);
Render it out to see the results which are what I think you are looking for.
print_r($a);
I can post a link if you want to learn how to sort multidimensional arrays by sub-array values if you need.

Changing array key value in PHP

I have an array like below,
[test] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 13
[4] => 32
[5] => 51
)
i need to change this array into like below,
[test] => Array
(
[2] => 1
[4] => 3
[6] => 5
[8] => 13
[10] => 32
[12] => 51
)
i need to change the key value. How can i do this?.
$newArray = array_combine(
range(2,count($originalArray)*2,2),
array_values($originalArray)
);
The array_values() function returns an array containing all the values of an array and also it reset all the keys. you can do it as
$arr = array(0 => 1, 1 => 3, 2 => 5, 3 => 13, 4 => 32, 5 => 51);
$count = 1;
$tempArr = array();
foreach ($arr as $key => $val) {
$tempArr[$count * 2] = $val;
$count++;
}
var_dump($tempArr);exit;
Try this code at your side.

How to combine both array and insert into database php

if only one array is there for example
$values = array(x, y, z);
i am adding them into database like this
foreach ($values as $value)
{
$insertFunction = addValues($value);
}
my arrays:
$array1 = Array ( 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 );
$array2 = Array ( fb1, or1, fb2, or2, fb3, or3, fb4, or4, fb5, or5 );
But i want both array to combine and insert them into database.
How can i do this please help me
Updated:
When i am printing the POST values i am getting out put like this
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 2 [4] => 1 [5] => 2 [6] => 1 [7] => 2 [8] => 1 [9] => 2 )
Array ( [0] => fb1 [1] => or1 [2] => fb2 [3] => or2 [4] => fb3 [5] => or3 [6] => fb4 [7] => or4 [8] => fb5 [9] => or5 )
when i tried with array_merge my out put is like this
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 2 [4] => 1 [5] => 2 [6] => 1 [7] => 2 [8] => 1 [9] => 2 [10] => fb1 [11] => or1 [12] => fb2 [13] => or2 [14] => fb3 [15] => or3 [16] => fb4 [17] => or4 [18] => fb5 [19] => or5 )
How to insert them in separate columns in a table $array1 and $array2
my database table is like this
1.id
2.username
3.network_id
id is primary key
network_id values coming in array1
username values coming in array2
EDIT:
After you mentioned seperated columns I think I understand what you're looking for:
I'm assuming that array1 and array2 are in the same size.
for($i = 0; $i < count($array1); $i++)
{
$array2[$i] = (int)$array2[$i]; //"validating" the username (an integer)
mysql_query("INSERT INTO yourTableName (`username`,`network_id`) VALUES('".$array2[$i]."','".$array1[$i]."')");
}
Result:
tblName:
username: 1 2 1 ...
network_id: fb1 or1 fb2 ...
Is that what you were looking for?
Ignore this and merging:
$combined = array_merge($array1 , $array2);
//$combined = Array ( 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,fb1, or1, fb2, or2, fb3, or3, fb4, or4, fb5, or5 );
I think you need array_merge.
You can use array_merge(), function to merge multiple array into one.
$arrays = array_merge($array1 , $array2);
foreach ($arrays as $value)
{
$insertFunction = addValues($value);
}
If you want associate an element from array a to an element from array b, you have to use array_combine() function.
$arrays = array_combine($array1,$array2);
foreach ($array as $aValue)
{
$insertFunction = addValues($aValue);
}

Categories