php get max key from same value - php

How can I get max key from same value?
Example:
1 => 32
2 => 32
3 => 32
4 => 5
5 => 5
6 => 11
7 => 11
Result:
3 => 32
5 => 5
7 => 11

Assuming values are all integer
$array = [
1 => 32,
2 => 32,
3 => 32,
4 => 5,
5 => 5,
6 => 11,
7 => 11,
];
$result = array_flip(array_flip($array));
var_dump($result);

krsort($array);
ksort(array_unique($array);

Related

SUM associative Array index dynamically on PHP

I have a result PHP array like this
$listResult = array(
0 => array(
0 => 1,
1 => 15,
2 => 4,
3 => 52
),
1 => array(
0 => 2,
1 => 0,
2 => 3,
3 => 2
),
2 => array(
0 => 3,
1 => 5,
2 => 9,
3 => 1
),
3 => array(
0 => 4,
1 => 10,
2 => 2,
3 => 5
),
4 => array(
0 => 1,
1 => 1,
2 => 2,
3 => 3
));
How to sum each keys? The result should be like this:
$sumResult = array(
0 => 11,
1 => 31,
2 => 20,
3 => 63
);
Already search in stackoverflow but the answer required key, in this case I just want to sum each index dynamically without a key. Sorry if my english is bad.
Use array_column() to get an array of all the elements with the same key, and array_sum() to sum them.
$sumResult = [];
foreach (array_keys($listResult[0]) as $key) {
$sumResult[$key] = array_sum(array_column($listResult, $key));
}

Array_combine error: puts out the wrong values and gives only the unique values php

hi all i have 2 arrays
[0] => CD Alaves
[1] => Granada CF
[2] => Getafe
[3] => CD Leganes
[4] => Barcelona
[5] => Getafe
[6] => Atletico Madrid
[7] => Getafe
[8] => Sevilla
[9] => Athletic Bilbao
[10] => CD Leganes
and their corresponding values
[0] => 11
[1] => 11
[2] => 11
[3] => 11
[4] => 11
[5] => 10
[6] => 10
[7] => 10
[8] => 10
[9] => 10
[10] => 9
I am currently using
$teamdata=array_combine($clubs,$stats);
echo "<pre>";
print_r($teamdata);
echo "</pre>";
however it outputs as
[CD Alaves] => 1
[Granada CF] => 1
[Getafe] => 1
[CD Leganes] => 1
[Barcelona] => -
[Atletico Madrid] => 1
[Sevilla] => 1
[Athletic Bilbao] => -
[Real Betis] => 1
[Espanyol] => 1
[Osasuna] => 1
[Villarreal] => 1
[Real Madrid] => 1
[Levante] => 1
giving unique values on the left and 1s on the right
it should read
[CD Alves]=>11
[Granada CF]=>11
....
Thanks a lot to anyone who sees this and takes the time to respond.
As long as your two originating arrays are valid, then array_combine works just fine:
<?php
$arr1 = array(
0 => "CD Alaves",
1 => "Granada CF",
2 => "Getafe",
3 => "CD Leganes",
4 => "Barcelona",
5 => "Getafe",
6 => "Atletico Madrid",
7 => "Getafe",
8 => "Sevilla",
9 => "Athletic Bilbao",
10 => "CD Leganes"
);
$arr2 = array(
0 => 11,
1 => 11,
2 => 11,
3 => 11,
4 => 11,
5 => 10,
6 => 10,
7 => 10,
8 => 10,
9 => 10,
10 => 9
);
$res = array_combine($arr1, $arr2);
print_r($res);
// Gives:
Array
(
[CD Alaves] => 11
[Granada CF] => 11
[Getafe] => 10
[CD Leganes] => 9
[Barcelona] => 11
[Atletico Madrid] => 10
[Sevilla] => 10
[Athletic Bilbao] => 10
)
A working example: http://sandbox.onlinephpfunctions.com/code/a6da80745eff9ff9b598fe4a2d7fbd8d71d3fb08
Even if your arrays are single values without explicit keys, it will work (php adds numeric indexes anyway)
<?php
$arr1 = array(
"CD Alaves",
"Granada CF",
"Getafe",
"CD Leganes",
"Barcelona",
"Getafe",
"Atletico Madrid",
"Getafe",
"Sevilla",
"Athletic Bilbao",
"CD Leganes"
);
$arr2 = array(
11,
11,
11,
11,
11,
10,
10,
10,
10,
10,
9
);
$res = array_combine($arr1, $arr2);
print_r($res);
// Prints the same output as above.
I had the exact same problem and this worked for me. Please mark this answer as accepted if it works for you.
Code from #axiac on https://stackoverflow.com/a/46671863/19402660
// The input arrays
$Array1 = ['bus', 'bus', 'int'];
$Array2 = [2, 18, 10];
// Build the result here
$Array3 = [];
// There is no validation, the code assumes that $Array2 contains
// the same number of items as $Array1 or more
foreach ($Array1 as $index => $key) {
// If the key $key was not encountered yet then add it to the result
if (! array_key_exists($key, $Array3)) {
$Array3[$key] = 0;
}
// Add the value associate with $key to the sum in the results array
$Array3[$key] += $Array2[$index];
}
print_r($Array3);
Its output:
Array
(
[bus] => 20
[int] => 10
)

How to randomly get min value from Array?

I have an array with multiple similar minimum value.
May I know how to randomly get one of the minimum value?
Here is my sample code:-
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$b = array_keys($aryNo, min($aryNo)); //Here only can get 1 value.
$intNo = $b[0];
May I know how to get min value list (3 => 12, 4 => 12,5 => 12,10 => 12) and randomly pick one of them so that I can set in $intNo?
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$b = array_keys($aryNo, min($aryNo)); //Here only can get 1 value.
// Taking a random KEY from $b
$key = array_rand($b);
// Taking a KEY from $aryNo which is under `$key`
echo $b[$key];
// Taking a VALUE from `$aryNo` which is under `$b[$key]`
echo $aryNo[$b[$key]];
The fiddle.
Try something like this:
$aryNo = [34,34,34,51,12,12,12,56,876,453,43,12];
foreach($aryNo as $a) {
$finalArray[$a][] = $a;
}
print("<pre>".print_r($finalArray,true)."</pre>");
$minKey = min(array_keys($finalArray));
print("<pre>".print_r($finalArray[$minKey],true)."</pre>");
$randIndex = array_rand($finalArray[$minKey]);
print_r("Key: ".$randIndex.", ".$finalArray[$minKey][$randIndex]);
First print prints:
Array
(
[34] => Array
(
[0] => 34
[1] => 34
[2] => 34
)
[51] => Array
(
[0] => 51
)
[12] => Array
(
[0] => 12
[1] => 12
[2] => 12
[3] => 12
)
[56] => Array
(
[0] => 56
)
[876] => Array
(
[0] => 876
)
[453] => Array
(
[0] => 453
)
[43] => Array
(
[0] => 43
)
)
Than you select min key, and that prints this:
Array
(
[0] => 12
[1] => 12
[2] => 12
[3] => 12
)
At the end you pick random key from this array and print the value:
Key: 2, Value: 12
`<?php
$sortArr = array();
$aryNo = array(0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,5 =>
12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,10 => 12);
asort($aryNo);
$aryNo = array_values($aryNo);
print_r($aryNo);
echo $aryNo[0];
?>`
I found that if I using shuffle(); also work for me.
Here is my example:-
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$aryNo2 = array_keys($aryNo, min($aryNo));
shuffle($aryNo2); //
$intWinNo = $aryNo2[0];
Thanks #u_mulder suggestion & answer.

Sort array using another array of values

I have the following array depicting points accumulated during the year. As a tie breaker on equal points, I have another value that I calculate that I want the result to be sorted on.
This is the sorted array, showing points during the year:
$results = [
1 => 220
0 => 209
4 => 127
14 => 89
3 => 84
7 => 78
2 => 71
13 => 61
16 => 56
8 => 48
12 => 45
10 => 42
11 => 42
6 => 39
5 => 35
9 => 32
15 => 22
17 => 22
18 => 22
19 => 1
Because indexes 10 and 11, and 15, 17 and 18 are equal, they need to be sorted by the lowest values in the following array:
// For 10 and 11
$anotherArray = [
11 => 101
10 => 119
]
// For 15, 17 and 18
$anotherArray = [
17 => 150
18 => 160
15 => 179
]
So the resulting array should look like this:
$finalArray = [
1 => 220
0 => 209
4 => 127
14 => 89
3 => 84
7 => 78
2 => 71
13 => 61
16 => 56
8 => 48
12 => 45
11 => 42
10 => 42
6 => 39
5 => 35
9 => 32
17 => 22
18 => 22
15 => 22
19 => 1
]
How would I achieve this?
EDIT: It's not the same as this question. The solution suggested is based on the values of an array and does not solve the issue where I need to insert values in another array in the correct order.
A 2-dimensional array is created with the values of the result array and the values of the 'anotherArrays'.
This array is sorted with uasort () and converted back into a one-dimensional array.
$results = [
1 => 220,0 => 209,4 => 127, 14 => 89, 3 => 84, 7 => 78,
2 => 71, 13 => 61, 16 => 56, 8 => 48, 12 => 45, 10 => 42, 11 => 42,
6 => 39, 5 => 35, 9 => 32, 15 => 22, 17 => 22, 18 => 22, 19 => 1
];
$anotherArray1 = [11 => 101, 10 => 119];
$anotherArray2 = [17 => 150, 18 => 160, 15 => 179];
$sekArr = array_replace($results,$anotherArray1,$anotherArray2);
$newArr = [];
foreach($results as $key => $value){
$newArr[$key] = ["v" => $value, 's' => $sekArr[$key]];
}
uasort($newArr, function($a,$b){
return $b['v'] <=> $a['v'] ?: $a['s'] <=> $b['s'];
});
$results = [];
foreach($newArr as $key => $arr){
$results[$key] = $arr['v'];
}
var_dump($results);
Try for yourself: https://3v4l.org/Si6ST

How to merge multidimentional array into single array?

I have this array which contains 2 keys and values..
array (
23 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
),
1 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 14,
8 => 12,
9 => 1,
10 => 7,
11 => 10,
12 => 8,
13 => 3,
)
)
So How can I convert this into single query with distinct values like this
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
20 => 14,
)
That means it should be first merge and then create distinct values array without using more foreach/for loop.
This is the code I have tried http://codepad.org/x881cBt1
You can use array_walk_recursive to flatten your multidimensional array:
$flatten = [];
array_walk_recursive($array, function ($value) use (&$flatten) {
$flatten[] = $value;
});
$flatten = array_unique($flatten); //Taking Unique for the flattened array
print_r($flatten);
This should give:
Array
(
[0] => 9
[1] => 13
[2] => 2
[3] => 11
[4] => 4
[5] => 5
[6] => 6
[7] => 12
[8] => 1
[9] => 7
[10] => 10
[11] => 8
[12] => 3
[20] => 14
)
Check EVAL
<?php
$data; //Your array
$data_set = array_values($data);
$required_data = [];
for ($i=0; $i< count($data_set); $i++) {
$required_data = array_merge($required_data, $data_set[$i]);
unset($data_set[$i]);
}
var_dump($required_data);
This will solve the issue
$arr;//Your array
$final_arr=array();
foreach($arr as $r){
$r=array_unique($r);
$final_arr=array_merge($final_arr,$r);
}
$final_arr=array_unique($final_arr);
print_r($final_arr);
see it live ideone
//Here 1st you have to Merge array & then remove duplicate entry
<?php
$main_array=array("0"=>array(1,2,3,4),"1"=>array(2,6,4),"2"=>array(2,8,5));
$temp_array=array();
for($i=0;$i<count($main_array);$i++)
{
$temp_array=array_merge($temp_array,$main_array[$i]);
}
//var_dump($temp_array);
$final_array=array_unique($temp_array);
echo "array:";
var_dump($final_array);
?>
you can use this code as you said this code is without any loop
$myArray = array (
23 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
),
1 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 14,
8 => 12,
9 => 1,
10 => 7,
11 => 10,
12 => 8,
13 => 3,
)
);
$objTmp = (object) array('array' => array());
array_walk_recursive($myArray, create_function('&$v, $k, &$t', '$t->array[] = $v;'), $objTmp);
print_r (array_unique($objTmp->array));
/* output
Array
(
[0] => 9
[1] => 13
[2] => 2
[3] => 11
[4] => 4
[5] => 5
[6] => 6
[7] => 12
[8] => 1
[9] => 7
[10] => 10
[11] => 8
[12] => 3
[20] => 14
)
*/
Thank you..

Categories