Loop through Array and sum up values with same id - php

I have a total black out. I have one array with n elements which has the results of a team, such as:
array(teamid, wins, losses, draws, goals);
array(1, 2, 3, 4, 5);
array(2, 2, 3, 4, 5);
array(1, 1, 2, 2, 6);
array(2, 2, 3, 4, 5);
I want to iterate through this array and sum up the values for each team-id in a second array. Such as:
$results = getResults();
$final = array();
foreach ($results as $result) {
foreach ($results as $res) {
if ($res['team_id'] == $result['team_id']) {
...
}
}
}
foreach ($final as $finalresult) {
...print result
}
In the end I want an array with e.g. in this example 2 values with 2 different team ids, each values summed up, but I have a blackout at the moment.
Does anybody have a solution?
Thanks.

You're running the loops nested, which means you're actually summing n^2 records. Assuming the array keys are the same between both arrays, then you'd only need a single loop:
$arr1 = array(...);
$arr2 = array(...);
$sum = 0;
foreach($arr1 as $key => $value) {
$sum += $arr1[$key] + $arr2[$key];
}
If the keys aren't the same,t hen you'll have to figure out to match up the members of the two arrays.

Your code is confusing, but I guess it will give a hint:
$results = getResults();
$final = array();
foreach ($results as $result) {
if(!isset($final[$result['team_id']])) {
$final[$result['team_id']] = $result['wins'];
} else {
$final[$result['team_id']] += $result['wins'];
}
}
foreach($final as $key=>$value) {
echo $key . ' ' . $value . '</br>';
}

Related

PHP array find two numbers are one after another

I have a sorted array with these stop_ids.
1,
6,
13,
18,
31,
I just want to find given first search value(6) is before the second given value(31). I tried something like this. That means the find order should be, first (6) then (13) not (13) first and (6) then.
foreach ($parent_array as $key => $value) {
$k = $key;
sort($routes); //another array with above values(stop_ids)
$st = 0;
foreach ($routes as $key => $value) {
if($st == 1){
unset($parent_array[$k]);
break;
}
elseif($value->stop_id == 31){
$st = 1;
continue;
}
}
}
return $parent_array;
I can provide two values. Here I used second value(31) only. Any help ???
Get array keys, under which is every number is located and compare this keys:
function firstNumberFirst($array, $first_number, $second_number)
{
return array_search($first_number, $array) < array_search($second_number, $array);
}
$a = [1, 6, 13, 18, 31];
var_dump(
firstNumberFirst($a, 6, 13),
firstNumberFirst($a, 6, 18),
firstNumberFirst($a, 13, 6)
);
If array is not zero-indexed - apply array_values first.

Keep unique node with lowest value on second index

I am working on a multi dimensional array in PHP. How is it possible to select unique values from 0 index of a nested array which has lowest value on index 1?
Below is my sample data array
$data = array (
array(1, 9),
array(1, 3),
array(1, 5),
array(9, 2),
array(9, 7),
);
I am trying to get following array as a result
$result = array (
array(1, 3),
array(9, 2),
);
One way to do this could be to loop the array, use the indexes of the arrays and compare the second value. If the stored second value is greater than the new value, then overwrite it.
$data = array (
array(1, 9),
array(1, 3),
array(1, 5),
array(9, 2),
array(9, 7),
);
$result = [];
foreach ($data as $d) {
if (!isset($result[$d[0]])) {
$result[$d[0]] = $d;
continue;
}
if ($result[$d[0]][1] > $d[1]) {
$result[$d[0]] = $d;
}
}
print_r($result);
Demo
$data = array (
array(1, 9),
array(1, 3),
array(1, 5),
array(9, 2),
array(9, 7),
);
$result = [];
$lowest0 = null;
$lowest1 = null;
foreach($data as $row){
if($lowest0 === null || $row[0] < $lowest0[0] || ($row[0] === $lowest0[0] && $row[1] < $lowest0[1]))
{
$lowest0 = $row;
}
if($lowest1 === null || $row[1] < $lowest1[1] || ($row[1] === $lowest1[1] && $row[0] < $lowest0[0]))
{
$lowest1 = $row;
}
}
$result = [$lowest0,$lowest1];
var_dump($result);
Short and dirty:
$data = array (
array(1, 9),
array(1, 3),
array(1, 5),
array(9, 2),
array(9, 7),
);
foreach ($data as $row) {
($row[1] < ($uniq[$row[0]][1] ?? INF)) and $uniq[$row[0]] = $row;
}
var_dump($uniq);
Pass resulting array through array_values if re-indexing from 0 is important to you
This is as clear as mud, but it I made it as an example that it works without looping.
In short I use min and array_intersect(_key) to find where the minimum values are in the columns (with array_column).
//split out the two columns
$col0 = array_column($data, 0);
$col1 = array_column($data, 1);
// find minimum value in each
$min0 = min($col0);
$min1 = min($col1);
// find what keys this minimum value is in of each array
$mincol0 = array_intersect($col0, [$min0]);
$mincol1 = array_intersect($col1, [$min1]);
// use the previous and compare with minimum value of the other column
$result[] = $data[array_keys(array_intersect_key($col1, $mincol0),min(array_intersect_key($col1, $mincol0)))[0]];
$result[] = $data[array_keys(array_intersect_key($col0, $mincol1),min(array_intersect_key($col0, $mincol1)))[0]];
Var_dump($result);
https://3v4l.org/dgbDK
Since I apperantly missunderstood the question.
Here is another answer, still no need to loop the full array. That is just a waste of time.
Loop only unique column 0 values and find the minmum value in column 1 using array_intersect and array_intersect_key.
$col0 = array_column($data, 0);
foreach(array_unique($col0) as $item){
$intersect = array_intersect($col0, [$item]);
$res[] = min(array_intersect_key($data, $intersect));
}
var_dump($res);
https://3v4l.org/3UPQh

Need Three Highest Values (or more, if tied) of PHP Array and Their Location?

I have an array. I'd like to get the three highest values of the array, but also remember which part of the array it was in.
For example, if my array is [12,3,7,19,24], my result should be values 24,19,12, at locations 4, 0, 3.
How do I do that? The first part is easy. Getting the locations is difficult.
Secondly, I'd like to also use the top three OR top number after three, if some are tied. So, for example, if I have [18,18,17,17,4], I'd like to display 18, 18, 17, and 17, at location 0,1,2,3.
Does that make sense? Is there an easy way to do that?
Wouldn't you be there using asort()?
For example:
<?php
$list = [4,18,18,17,17];
// Sort maintaining indexes.
asort($list);
// Slice the first 3 elements from the array.
$top3 = array_slice($list, -3, null, true);
// Results in: [ 1 => 18, 2 => 18, 3 => 17 ]
Or you can use arsort
function getMyTop($list, $offset, $top) {
arsort($list);
return array_slice($list, $offset, $top, true);
}
$myTop = getMyTop($list, 0, 3);
$myNextTop = getMyTop($list, 3, 4);
This is what you need!
<?php
$array = array(12,3,7,19,24);
$array_processed = array();
$highest_index = 0;
while($highest_index < 3)
{
$max = max($array);
$index = array_search($max,$array);
$array_processed[$index] = $max;
unset($array[$index]);
$highest_index++;
}
print_r($array_processed);
?>
You will get Index as well as the value! You just have to define how many top values you want! Let me know if it's what you want!
function top_three_positions($array){
// Sort the array from max to min
arsort($array);
// Unset everything in sorted array after the first three elements
$count = 0;
foreach($array as $key => $ar){
if($count > 2){
unset($array[$key]);
}
$count++;
}
// Return array with top 3 values with their indexes preserved.
return $array;
}
You can use a loop to determine how many elements your top-three-with-ties will have, after applying arsort:
function getTop($arr, $num = 3) {
arsort($arr);
foreach(array_values($arr) as $i => $v) {
if ($i >= $num && $v !== $prev) return array_slice($arr, 0, $i, true);
$prev = $v;
}
return $arr;
}
// Sample input
$arr = [4,18,17,6,17,18,9];
$top = getTop($arr, 3);
print_r($top); // [5 => 18, 1 => 18, 4 => 17, 2 => 17]
try this:
public function getTopSortedThree(array $data, $n = 3, $asc = true)
{
if ($asc) {
uasort($data, function ($a, $b) { return $a>$b;});
} else {
uasort($data, function ($a, $b) { return $a<$b;});
}
$count = 0;
$result = [];
foreach ($data as $key => $value) {
$result[] = $data[$key];
$count++;
if ($count >= $n){
break;
}
}
return $result;
}
Send false for desc order and nothing for asc order
Send $n with number of top values you want.
This functionality doesn't losing keys.
This task merely calls for a descending sort, retention of the top three values, and in the case of values after the third-positioned value being equal to the third value, retain these as well.
After calling rsort(), call a for() loop starting from the fourth element ([3]). If the current value is not equal to the value in the third position, stop iterating, and isolate the elements from the front of the array to the previous iteration's index. Done.
p.s. If the input array has 3 or fewer elements, the for() loop is never entered and the whole (short) array avoids truncation after being sorted.
Code: (Demo)
$array = [18, 17, 4, 18, 17, 16, 17];
rsort($array);
for ($i = 3, $count = count($array); $i < $count; ++$i) {
if ($array[2] != $array[$i]) {
$array = array_slice($array, 0, $i);
break;
}
}
var_export($array);
Because the loop purely finds the appropriate finishing point of the array ($i), this could also be compacted to: (Demo)
rsort($array);
for ($i = 3, $count = count($array); $i < $count && $array[2] === $array[$i]; ++$i);
var_export(array_slice($array, 0, $i));
Or slightly reduced further to: (Demo)
rsort($array);
for ($i = 3; isset($array[2], $array[$i]) && $array[2] === $array[$i]; ++$i);
var_export(array_slice($array, 0, $i));
Output:
array (
0 => 18,
1 => 18,
2 => 17,
3 => 17,
4 => 17,
)

PHP Script and max() Value

Sorry for my bad English and thanks for your help in advance! I have kind of a tricky problem I've encountered while coding. Here's the point:
I need a script that essentially extracts the 5 max values of 5 arrays, that are "mixed", i.e. they contain "recurrent" values. Here is an example:
array1(a, b)
array2(a, c, d, e, g)
array3(b, d, g, h)
array4(e, t, z)
array5(b, c, d, k)
The 2 essential requests are:
1) the sum of those 5 arrays (array1+array2+array3...) MUST be the highest possible...
2) ...without repeat ANY value previously used** (e.g. if in array1 the top value was "b", this cannot be re-used as max value in arrays 3 or 5).
Currently I have this...:
$group1 = array(a, b);
$group = array(a, b, c, d);
$max1a = max(group1);
$max2a = max(group2) unset($max1a);
$sum1 = $max1a + $max2a;
$max2b = max(group2);
$max1b = max(group1)
unset($max2b);
$sum2 = $max1b + $max2b;
if($sum1 > $sum2) {
echo $sum1
} else {
echo $sum2
}
... but it's kinda impossible to use this code with 5 arrays, because I should compare 5! (120...!!!) combinations in order to find the max sum value.
I know the problem is quite difficult to explain and to solve, but I really need your help and I hope you can save me!!!
Cheers
I'm adding this as another answer to leave the previous one intact for someone coming across this looking for that variation on this behaviour.
Given the 2 arrays:
$array1 = array(30, 29, 20);
$array2 = array(30, 20, 10);
The maximum sum using one element from each is 59 - this is dramatically different to my previous approach (and the answers' of others) which took the max element of the first array and then the highest element of the next array that is not equal to any previously used value - this would give 50 instead.
The code you want is this:
$mainArray = array();
$mainArray[] = array(30, 29, 20);
$mainArray[] = array(30, 20, 10);
$tempArray = array();
$newArray = array();
foreach($mainArray as $innerArray) {
$newArray = array();
if (count($tempArray) == 0) {
foreach ($innerArray as $value) {
$newArray[] = array('total' => $value, 'used' => array($value));
}
}
else {
foreach ($tempArray as $key => $innerTempArray) {
$placed = FALSE;
foreach ($innerArray as $value) {
if (!(in_array($value, $innerTempArray['used']))) {
$newArray[] = array('total' => $tempArray[$key]['total'] + $value, 'used' => $tempArray[$key]['used']);
$newArray[count($newArray) - 1]['used'][] = $value;
$placed = TRUE;
}
}
if (!($placed)) {
echo "An array had no elements that had not already been used";
die();
}
}
}
$tempArray = $newArray;
}
$total = 0;
if (count($newArray) == 0) {
echo "No data passed";
die();
}
else {
$total = $newArray[0]['total'];
}
for ($i = 0; $i < count($newArray); $i++) {
if ($newArray[$i]['total'] > $total) {
$total = $newArray[$i]['total'];
}
}
var_dump($total);
EDIT - Do not repeat used variables (but repeated values are ok):
Let
//$a = 30, $b = 30, $c = 25, $d = 20, $e = 19
$array1 = array($a, $c, $d);
$array2 = array($b, $d, $e);
We want to choose $a from $array1 and $b from $array2 as these give the largest sum - although they're values are the same that is allowed because we only care if the names of the variables saved to that place are the same.
With the arrays in the above format there is no way of achieving the desired behaviour - the arrays do not know what the name of the variable who's value was assigned to their elements, only it's value. Therefore we must change the first part of the original answer to:
$mainArray[] = array('a', 'c', 'd');
$mainArray[] = array('b', 'd', 'e');
and also have either the of the following before the first foreach loop (to declare $a, $b, $c, $d, $e)
//either
extract(array(
'a' => 30,
'b' => 30,
'c' => 25,
'd' => 20,
'e' => 19
));
//or
$a = 30; $b = 30; $c = 25; $d = 20; $e = 19;
The above both do exactly the same thing, I just prefer the first for neatness.
Then replace the line below
$newArray[] = array('total' => $value, 'used' => array($value));
with
$newArray[] = array('total' => ${$value}, 'used' => array($value));
The change is curly brackets around the first $value because that is then evaluated to get the variable name to use (like below example):
$test = 'hello';
$var = 'test';
echo ${$var}; //prints 'hello'
A similar change replaces
$newArray[] = array('total' => $tempArray[$key]['total'] + $value, 'used' => $tempArray[$key]['used']);
with
$newArray[] = array('total' => $tempArray[$key]['total'] + ${$value}, 'used' => $tempArray[$key]['used']);
Now the code will function as wanted :)
If you are dynamically building the arrays you are comparing and can't build the array of strings instead of variables then there is no way to do it. You would need some way of extracting "$a" or "a" from $a = 30, which PHP is not meant to do (there are hacks but they are complicated and only work in certain situations (google "get variable name as string in php" to see what I mean)
If by the top value you mean the first alphabetically then the following would work:
$array1 = array('a', 'b');
$array2 = array('a', 'c', 'd', 'e', 'g');
$array3 = array('b', 'd', 'g', 'h');
$array4 = array('e', 't', 'z');
$array5 = array('b', 'c', 'd', 'k');
$mainArray = array($array1, $array2, $array3, $array4, $array5);
foreach ($mainArray as $key => $value) {
sort($mainArray[$key]);
}
$resultArray = array();
foreach($maniArray as $key1 => $value1) {
$placed = FALSE;
foreach ($value1 as $value2) {
if (!(in_array($value2, $resultArray))) {
$resultArray[] = $value2;
$placed = TRUE;
break;
}
}
if (!($placed)) {
echo "All the values in the " . ($key + 1) . "th array are already max values in other arrays";
die();
}
}
var_dump($resultArray);
I'm not sure, of i really understood your problem correctly, these are my assumptions:
You have five arrays containing numbers
These numbers can occur multiple times across the arrays
You want to find the highest possible sum of elements across your arrays
The sum uses one single value of each array
But the sum must not use the same number twice
Is that correct?
If Yes, then:
The highest possible sum across all arrays is always the sum of the largest elements. If you do not want to use the same number twice, you can just get the maximum from the first array, remove it from all the others and then sum up all the remaining maxima.
Like so:
$arrays = array();
$arrays[] = array(1, 2);
$arrays[] = array(1, 3, 4, 5, 7);
$arrays[] = array(2, 4, 7, 8);
$arrays[] = array(5, 20, 26);
$arrays[] = array(2, 3, 4, 11);
for($i=0, $n=count($arrays); $i<$n; $i++) {
if($i===0) {
$a1max = max($arrays[$i]);
$sum = $a1max;
} else {
$duplicate_pos = array_search($a1max, $arrays[$i]);
if($duplicate_pos !== FALSE) {
unset($arrays[$i][$duplicate_pos]);
}
$sum += max($arrays[$i]);
}
}
echo "sum: " . $sum . "\n";
Assuming you have grouped together all your values in one array like this,
$array = array(
array(1,2,3),
array(1,2,3,4),
array(1,2,3,4,5,6),
array(1,2,3,4,5,6),
array(1,2,3,4,5,6,7)
);
Loop through $array, and get the highest value which has not been used previously,
$max = array();
foreach($array as $value)
$max[] = max(array_diff($value, $max));
Calculate the sum of all values with array_sum(),
echo "The maximal sum is: ".array_sum($max);

How can I remove ALL duplicates from an array in PHP?

First of all, I'd like to point out to all you duplicate question hunters that this question does not fully answer my question.
Now, I've got an array. We'll say that the array is array(1, 2, 2, 3, 4, 3, 2)
I need to remove the duplicates. Not just one of the duplicates, but all, so that the result will be array(1, 4)
I looked at array_unique(), but that will only result in array(1, 2, 3, 4)
Any ideas?
You could use the combination of array_unique, array_diff_assoc and array_diff:
array_diff($arr, array_diff_assoc($arr, array_unique($arr)))
function removeDuplicates($array) {
$valueCount = array();
foreach ($array as $value) {
$valueCount[$value]++;
}
$return = array();
foreach ($valueCount as $value => $count) {
if ( $count == 1 ) {
$return[] = $value;
}
}
return $return;
}

Categories