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
Related
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!
I have an array where I want to group items having match strings in key.
My array looks like this:
Array
(
[ALL_trading_enabled] => 1
[ALL_enabled_pairs] => ALL
[ALL_max_trading_pairs] => 10
[SNGLSBTC_DCA_enabled] =>
[SNGLSBTC_sell_only_mode] => 1
[SNGLSBTC_sell_value] => 0.28
[SNGLSBTC_trailing_profit] => 0.009
[ENJBTC_DCA_enabled] =>
[ENJBTC_sell_only_mode] => 1
[ENJBTC_sell_value] => 0.28
[ENJBTC_trailing_profit] => 0.009
[BCPTBTC_DCA_enabled] =>
[BCPTBTC_sell_only_mode] => 1
[BCPTBTC_sell_value] => 0.28
[BCPTBTC_trailing_profit] => 0.009
)
I want to group the items that have the same string. What I want looks like this:
Array
(
[0] => Array(
[ALL_trading_enabled] => 1
[ALL_enabled_pairs] => ALL
[ALL_max_trading_pairs] => 10
)
[1] => Array(
[SNGLSBTC_DCA_enabled] =>
[SNGLSBTC_sell_only_mode] => 1
[SNGLSBTC_sell_value] => 0.28
[SNGLSBTC_trailing_profit] => 0.009
)
[2] => Array(
[ENJBTC_DCA_enabled] =>
[ENJBTC_sell_only_mode] => 1
[ENJBTC_sell_value] => 0.28
[ENJBTC_trailing_profit] => 0.009
)
[3] => Array(
[BCPTBTC_DCA_enabled] =>
[BCPTBTC_sell_only_mode] => 1
[BCPTBTC_sell_value] => 0.28
[BCPTBTC_trailing_profit] => 0.009
)
)
Any help to achieve this? Or better yet if I can assign the match as the key for the created group.
Array(
[ALL] => Array(
//items here
)
[SNGLSBTC] => Array(
//items here
)
)
Explode on _ and get the first portion, use as the key and add to that array:
foreach($array as $key => $value) {
$new_key = explode('_', $key)[0];
$result[$new_key][$key] = $value;
}
If needed to re-index (after your edit you don't need this):
$result = array_values($result);
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))
I have a dummy array, that I want to order.
How can I have following result
with foreach?
with while next()
with RecursiveIterator
with IteratorIterator
which one is the fastest?
Here is array
$files = array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'wp-config.php',
'wp-content' =>
array (
'uploads' =>
array (
2013 =>
array (
'05' =>
array (
0 => 'kabeduvarkad-1024x768.jpg',
1 => 'kabeduvarkad-150x150.jpg',
2 => 'kabeduvarkad-300x225.jpg',
3 => 'kabeduvarkad-940x198.jpg',
),
10 =>
array (
),
),
2014 =>
array (
'02' =>
array (
),
),
2015 => 'de.php',
),
),
'wp-update' =>
array (
0 => 'wp-update.tar',
1 => 'wp-update.tar.gz',
2 => 'wp-update1.tar',
3 => 'wp-update1.tar.gz',
),
4 => 'wp-update.tar.gz',
);
Expected Result
$expected = array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'test.php',
4 => 'wp-config.php',
5 => 'wp-content/',
6 => 'wp-content/uploads/',
7 => 'wp-content/uploads/2013/',
8 => 'wp-content/uploads/2013/05/',
9 => 'wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg',
10 => 'wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg',
11 => 'wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg',
12 => 'wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg',
13 => 'wp-content/uploads/2013/05/kabeduvarkad.jpg',
14 => '...'
);
array_walk_recursive is what you're looking for.
Example (not tested):
$expected = [];
array_walk_recursive($your_array, function($item, $key) {
// push item on to $expected
$expected[] = $item;
});
Working example
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.