GET Max array from two array PHP - php

I want which is the biggest array from following array.
[13] => Array
(
[0] => 1
[1] => 3
[2] => 9
)
[15] => Array
(
[0] => 1
[1] => 5
[2] => 8
)
[33] => Array
(
[0] => 1
[1] => 9
[2] => 13
)
I want a code that would return last array with key 33.
Please Help.

Use max to get the maximum from the keys of your array
$yourarray=array(13 => Array
(
0 => 1,
1 => 3,
2 => 9,
),
15 => Array
(
0 => 1,
1 => 5,
2 => 8,
),
33 => Array
(
0 => 1,
1 => 9,
2 => 13,
));
$arr=max(array_keys($yourarray));
print_r($yourarray[$arr]);
Output:
Array
(
[0] => 1
[1] => 9
[2] => 13
)

This should do the trick...
<?php
$tests = array(
13 => array(1,3,9),
15 => array(1,5,8),
33 => array(1,9,13)
);
$array_totals = array_map('array_sum', $tests);
krsort($array_totals);
$maxArray = each($array_totals);
var_dump($maxArray);
Gives
array (size=4)
1 => int 23
'value' => int 23
0 => int 33
'key' => int 33

Not the most beautiful thing, but readable ;)
$tests = array(
13 => array(1,3,9),
15 => array(1,5,8),
33 => array(1,9,13)
);
$max = -1;
$max_key = -1;
foreach ($tests as $k => $v) {
$cur_max = max($v);
if ($cur_max >= $max) {
$max = $cur_max;
$max_key = $k;
}
}
echo "max: $max; max_key: $max_key";
Gives:
max: 13; max_key: 33
To make it more beautiful: use array_map and sorting.

Related

sum array indexes with same value for a specific key

I have the following array:
[0] => Array
(
[id] => 1
[uid] => 50
[sum1] => 1
[sum2] => 2
)
[1] => Array
(
[id] => 2
[uid] => 50
[sum1] => 2
[sum2] => 4
)
[2] => Array
(
[id] => 3
[uid] => 51
[sum1] => 3
[sum2] => 5
)
As you can see, on some of those indexes, [uid] is the same. The length and data of the array is dynamic.
what i need to do is merge the indexes that have the same value for[uid] and sum the values for the other keys:
[0] => Array
(
[id] => 2
[uid] => 50
[sum1] => 3
[sum2] => 6
)
[1] => Array
(
[id] => 3
[uid] => 51
[sum1] => 3
[sum2] => 5
)
But for the life of me, i can't figure out how to do that.
Any help appreciated!
You can do it like this way,
<?php
$mainArray = [
0 => Array
(
'id' => 1,
'uid' => 50,
'sum1' => 1,
'sum2' => 2
),
1 => Array
(
'id' => 2,
'uid' => 50,
'sum1' => 2,
'sum2' => 4
),
2 => Array
(
'id' => 3,
'uid' => 51,
'sum1' => 3,
'sum2' => 5
)
];
$newArray=[];
foreach ($mainArray as $value) {
if(!array_key_exists($value['uid'], $newArray)){
// Store first time
$newArray[$value['uid']] = $value;
}else{
// Already exist, then sum and replace it
$newArray[$value['uid']]['id'] = $value['id'];
$newArray[$value['uid']]['sum1'] += $value['sum1'];// Sum with previous value
$newArray[$value['uid']]['sum2'] += $value['sum2'];// Sum with previous value
}
}
$newArray = array_values($newArray);// Reset indexes, Start the array index with zero
print_r($newArray);// To see the output
Output:
Array
(
[0] => Array
(
[id] => 2
[uid] => 50
[sum1] => 3
[sum2] => 6
)
[1] => Array
(
[id] => 3
[uid] => 51
[sum1] => 3
[sum2] => 5
)
)
I think you are correct. Actually, i solved the problem using something similar:
$sumArray = Array();
foreach ($array1 as $item){
if(isset($sumArray[$item['uid']])){
$sumArray[$item['idPartener']]['sum1'] += $item['sum1'];
$sumArray[$item['idPartener']]['sum2'] += $item['sum2'];
}else{
$sumArray[$item['uid']] = Array(
'id' => $item['id'],
'sum1' => $item['sum1'],
'sum2' => $item['sum2'] ,
);
}
}
Thanks all for your help!

How to remove array if all keys acquired by another array in multidimensional array PHP

can you please help me out in this.
What will be the function to remove 4th and 5th array in PHP.
Array
(
[0] => Array
(
[10] => 98
[11] => 1
[433438] => 8
)
[1] => Array
(
[10] => 98
[11] => 1
[433438] => 1
)
[2] => Array
(
[13] => 98
[11] => 2
[433438] => 8
)
[3] => Array
(
[14] => 98
[11] => 2
[433438] => 1
)
[4] => Array
(
[10] => 18
[11] => 1
)
[5] => Array
(
[14] => 18
[11] => 2
)
)
Thanks in Advance.
$base = [
[10 => 98, 11 => 1, 433438 => 8],
[10 => 98, 11 => 1, 433438 => 1],
[13 => 98, 11 => 2, 433438 => 8],
[14 => 98, 11 => 2, 433438 => 1],
[10 => 18, 11 => 1],
[14 => 18, 11 => 2],
];
$invalid = [];
for ($i = 0; $i <= count($base) - 1; $i++) {
for ($j = 0; $j <= count($base) - 1; $j++) {
$refCount = count($base[$j]);
$interSectCount = count(array_intersect(array_keys($base[$i]), array_keys($base[$j])));
if (count($base[$i]) !== $refCount && $interSectCount === $refCount) {
$invalid[] = $j;
}
}
}
foreach ($invalid as $item) {
unset($base[$item]);
}
You can use array_pop() function which eliminates the last element of the array but if you use it two times you will get the desired result as
<?php
$data = [
1 => [1,2],
2 => [1,3],
3 => [1,4]
];
$value = array_pop($data);
$value = array_pop($data);
?>
Output
Array
(
[1] => Array
(
[0] => 1
[1] => 2
)
)

I want to add one array key and value with another array values

I want to add one array key and value with another array's value.
If I have arrays as follow:
Array ( [6] => 12 [8] => 9 [10] => 11 )
Array ( [6] => 70 [8] => 10 [9] => 35 [10] => 25 [11] => 25 [12] => 2 )
For example (6 => 12 when processed result in 70 + 2 )
This is the expected output with the two arrays above:
array ([0] => 72 [1] => 45 [2] => 50 )
Thanks In Advance
Use the following simple solution:
$arr1 = [6 => 12, 8 => 9 , 10 => 11];
$arr2 = [6 => 70, 8 => 10, 9 => 35, 10 => 25, 11 => 25, 12 => 2];
$result = [];
foreach ($arr1 as $k => $v) {
if (isset($arr2[$k]) && isset($arr2[$v])) {
$result[] = $arr2[$k] + $arr2[$v];
}
}
print_r($result);
The output:
Array
(
[0] => 72
[1] => 45
[2] => 50
)
$array1 = Array ( 6 => 12, 8 => 9, 10 => 11 );
$array2 = Array ( 6 => 70, 8 => 10, 9 => 35, 10 => 25, 11 => 25, 12 => 2 );
$newarray = array();
foreach ($array1 as $key => $item) {
$newarray[] = $array2[$item] + $array2[$key];
}
var_dump($newarray);

Count multi array rates

Im having troubles counting this.
I want to count all rates than belongs to id_image.
Maybe like key = id_image and value = tot count, id tried with array_count_values, but i cant use it normally when its multi :-S
Array
(
[0] => Array
(
[id_image] => 12
[rate] => 4
)
[1] => Array
(
[id_image] => 13
[rate] => 4
)
[2] => Array
(
[id_image] => 14
[rate] => 3
)
[3] => Array
(
[id_image] => 13
[rate] => 4
)
[4] => Array
(
[id_image] => 12
[rate] => 5
)
[5] => Array
(
[id_image] => 12
[rate] => 4
)
)
// test array
$arr = array(
0 => array(
'id_image' => 1,
'rate' => 3
),
1 => array(
'id_image' => 2,
'rate' => 8
),
2 => array(
'id_image' => 3,
'rate' => 4
),
3 => array(
'id_image' => 1,
'rate' => 2
),
4 => array(
'id_image' => 3,
'rate' => 2
)
);
// put the length in a var so we don't keep calling count();
$length = count($arr);
// the new array that'll hold the sum of the rates
$totals = array();
// iterate through the test array
for ($i = 0; $i < $length; ++$i) {
// check if $totals already contains data for the specified id_image
if (isset($totals[$arr[$i]['id_image']])) {
// if so, add data
$totals[$arr[$i]['id_image']] += $arr[$i]['rate'];
} else {
// if not so, set data
$totals[$arr[$i]['id_image']] = $arr[$i]['rate'];
}
}
var_dump($totals);
Example

unique array on base on value of specific key

I have the following array:
Array
(
[0] => Array
(
[hotelID] => 10
[hotelcategoryID] => 12
[hotelName] => Grand Forest Metsovo
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 2
)
[1] => Array
(
[hotelID] => 10
[hotelcategoryID] => 12
[hotelName] => Grand Forest Metsovo
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 3
)
[2] => Array
(
[hotelID] => 10
[hotelcategoryID] => 12
[hotelName] => Grand Forest Metsovo
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 4
)
[3] => Array
(
[hotelID] => 14
[hotelcategoryID] => 7
[hotelName] => Hotel Metropolis
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 23
)
[4] => Array
(
[hotelID] => 14
[hotelcategoryID] => 7
[hotelName] => Hotel Metropolis
[hotelShortDescription] =>
[hotelVisible] => 1
[roomID] => 24
)
)
I have two different hotelID keys. I would like to extract only one element (the first one) where the hotelID is unique in whole array. I am trying with following code:
$data['uniqueHotels'] = array_map('unserialize', array_unique(array_map('serialize', $hotels)));
but without any luck so far.
Anyone can give me a hint?
If looking for the first element:
<?php
$hotels = array(
array(
'id' => 1,
'hotelID' => 10
),
array(
'id' => 2,
'hotelID' => 10,
),
array(
'id' => 3,
'hotelID' => 20,
),
array(
'id' => 4,
'hotelID' => 20,
),
);
function getUniqueHotels($hotels) {
$uniqueHotels = array();
foreach($hotels as $hotel) {
$niddle = $hotel['hotelID'];
if(array_key_exists($niddle, $uniqueHotels)) continue;
$uniqueHotels[$niddle] = $hotel;
}
return $uniqueHotels;
}
$unique_hotels = getUniqueHotels($hotels);
print_r($unique_hotels);
results in:
Array
(
[10] => Array
(
[id] => 1
[hotelID] => 10
)
[20] => Array
(
[id] => 3
[hotelID] => 20
)
)
You could simply loop through the array and add them to a new array, indexed by hotelID. This way any duplicates will just overwrite the existing value and you end up with one entry per hotel:
$unique = array();
foreach ($hotels as $value)
{
$unique[$value['hotelID']] = $value;
}
$data['uniqueHotels'] = array_values($unique);
Here is a dynmaic solution:
function uniqueAssocArray($array, $uniqueKey){
$unique = array();
foreach ($array as $value){
$unique[$value[$uniqueKey]] = $value;
}
$data = array_values($unique);
return $data;
}
How to use:
uniqueAssocArray($yourArray, 'theKey');
along the lines of what you're trying,
array_unique(array_map(function($hotel) { return $hotel['hotelID']; }, $array))

Categories