php - Sum array value per day in multidimensional array - php

There are a number of great Q&As on Stackoverflow on how to sum across a multidimensional associative array but I have not found a working example of doing subtotals within a multidimensional one.
For instance I have data coming out of a mysql query into php with this shape:
$myArray = array(
'2014-4-3' => 2,
'2014-4-4' => 3,
'2014-4-5' => array(
0 => 3,
1 => 7,
2 => 7,
3 => 7
)
);
Essentially, I am pulling the ratings made of restaurants by day. Some days might have many ratings and others will have fewer (those days with no ratings are omitted from the array). On days with more ratings I would like to sum up to a total for that given day so a new array would look simply as follows:
'2014-4-3' => 2
'2014-4-4' => 3
'2014-4-5' => 24
I have tried for hours to hack the foreach and functions approaches posted for summing multidimensional arrays but nothing so far. One key problem is that the days themselves aren't known in advance as each day is added the same process must be expanded.

You could do this using array_map() and array_sum():
$output = array_map(function($a) {
return is_array($a) ? array_sum($a) : $a;
}, $myArray);
Here's a demo

You can simply use nested foreach with is_array()
$myArray = array(
'2014-4-3' => 2,
'2014-4-4' => 3,
'2014-4-5' => array(
0 => 3,
1 => 7,
2 => 7,
3 => 7
)
);
$new=array();
foreach($myArray as $day=>$value){
$newval =0;
if(is_array($value)){
foreach ($value as $val) {
$newval += $val;
}
}else{
$newval = $value;
}
$new[$day]=$newval;
}
var_dump($new);

You can try this one:
foreach($myArray as &$value){
if(is_array($value)){
$value = array_sum($value);
}
}

Related

array_slice for associative array

I have an array with indexes as timestamps.
I tried array_slice to get all values between a time range but it doesn't seem to work.
$data = array_slice(["1549440811" => 1, "1549448226" => 2, "1551108588" => 3 ], 0, 1549460338);
I should get the $data as ["1549440811" => 1, "1549448226" => 2] but it doesn't work that way.
To get the right data I have to use
$data = array_slice(["1549440811" => 1, "1549448226" => 2, "1551108588" => 3 ], 0, 2);
But the problem is the records can have random timestamps and no. of records. So I am unable to figure out the offset which is 2 in this case.
I know the code below with a few changes might work for small range but not for my timestamps as $myrange would have a lot of data.
$myrange = range(0,1549460338);
$output = array_intersect(["1549440811" => 1, "1549448226" => 2, "1551108588" => 3 ] , $myrange );
I am avoiding looping through the array as the array has a lot of data. Also I have a lot of timestamps to check. This code is a simplified logic of a bigger code with records from database indexed with timestamps.
Is there any other way I could get the desired data?
Simple for-loop should do:
$arr = ["1549440811" => 1, "1549448226" => 2, "1551108588" => 3 ];
$range = "1549460338";
foreach($arr as $k => $v) {
if ($range > $k)
break;
$newArr[$k] = $v;
}
You can also use array_filter (doc):
$filtered = array_filter( $arr,
function ($key) use ($range) {return $range > $key;},
ARRAY_FILTER_USE_KEY
);
Example: 3v4l
Edit:
Fastest way (consider your array is sorted) is to extract the keys with $keys = array_keys($arr); and then search for the $range using binary search (O(log(n))) -> then use array_slice with that index.

Match 2 arrays keys and show value of second array

I have two arrays that I need to match and post the result.
$arr1 = 1,3,4;
$arr2 = 1 => 'Title1'
2 => 'Title2'
3 => 'Title3'
4 => 'Title4'
I want to get the result to show the matched keys (number matches) to then show the value of $arr2
The above should result in Title1, Title3, title4
I have tried array_key_exists but i think im getting myself confused over something simple.
foreach($arr1 as $a)
{
if(array_key_exists($a, $arr2))
{
$new[$a] = $arr2[$a];
}
}
$l=1;
foreach($new as $n => $ob){
echo '<p>'.$new[$l][0].'">'.$new[$l][0].'</p>';
++$l;
}
In your case you can use like this
$arr1 = [1,3,4];
$arr2 = [1 => 'Title1', 2 => 'Title2', 3 => 'Title3', 4 => 'Title4'];
foreach($arr1 as $key){
echo "\n". $arr2[$key];
}
You can also check existence by isset first
Live demo : https://eval.in/720926
In your code, where does $d come from ? It should be $a.
And $n sould be used instead of $l (which is useless) and thus, not incremented.
To simplify your code, you could have written :
<?php
foreach($arr1 as $k){
if(array_key_exists($k, $arr2)){
// or : if(isset($arr2[$k])){
echo $arr2[$k] . "\n";
}
}
?>

Shuffle the order of keys in an associative array, if they have the same values?

Given an associative array like this, how can you shuffle the order of keys that have the same value?
array(a => 1,
b => 2, // make b or c ordered first, randomly
c => 2,
d => 4,
e => 5, // make e or f ordered first, randomly
f => 5);
The approach I tried was to turn it into a structure like this and shuffle the values (which are arrays of the original keys) and then flatten it back into the original form. Is there a simpler or cleaner approach? (I'm not worried about efficiency, this is for small data sets.)
array(1 => [a],
2 => [b, c], // shuffle these
4 => [d],
5 => [e, f]); // shuffle these
function array_sort_randomize_equal_values($array) {
$collect_by_value = array();
foreach ($array as $key => $value) {
if (! array_key_exists($value, $collect_by_value)) {
$collect_by_value[$value] = array();
}
// note the &, we want to modify the array, not get a copy
$subarray = &$collect_by_value[$value];
array_push($subarray, $key);
}
arsort($collect_by_value);
$reordered = array();
foreach ($collect_by_value as $value => $array_of_keys) {
// after randomizing keys with the same value, create a new array
shuffle($array_of_keys);
foreach ($array_of_keys as $key) {
array_push($reordered, $value);
}
}
return $reordered;
}
I rewrote the entire code, since I found another way which is a lot simpler and faster than the old one(If you are still interested in the old one see the revision):
old code (100,000 executions): Ø 4.4 sec.
new code (100,000 executions): Ø 1.3 sec.
Explanation
First we get all unique values from the array with array_flip(), since then the values are the keys and you can't have duplicate keys in an array we have our unique values. We also create an array $result for then storing our result in it and $keyPool for storing all keys for each value.
Now we loop through our unique values and get all keys which have the same value into an array with array_keys() and save it in $keyPool with the value as key. We can also right away shuffle() the keys array, so that they are already random:
foreach($uniqueValues as $value => $notNeeded){
$keyPool[$value] = array_keys($arr, $value, TRUE);
shuffle($keyPool[$value]);
}
Now we can already loop through our original array and get a key with array_shift() from the $keyPool for each value and save it in $result:
foreach($arr as $value)
$result[array_shift($keyPool[$value])] = $value;
Since we already shuffled the array the keys already have a random order and we just use array_shift(), so that we can't use the key twice.
Code
<?php
$arr = ["a" => 1, "b" => 1, "c" => 1, "d" => 1, "e" => 1, "f" => 2,
"g" => 1, "h" => 3, "i" => 4, "j" => 5, "k" => 5];
function randomize_duplicate_array_value_keys(array $arr){
$uniqueValues = array_flip($arr);
$result = [];
$keyPool = [];
foreach($uniqueValues as $value => $notNeeded){
$keyPool[$value] = array_keys($arr, $value, TRUE);
shuffle($keyPool[$value]);
}
foreach($arr as $value)
$result[array_shift($keyPool[$value])] = $value;
return $result;
}
$result = randomize_duplicate_array_value_keys($arr);
print_r($result);
?>
(possible) output:
Array (
[b] => 1
[g] => 1
[a] => 1
[e] => 1
[d] => 1
[f] => 2
[c] => 1
[h] => 3
[i] => 4
[k] => 5
[j] => 5
)
Footnotes
I used array_flip() instead of array_unique() to get the unique values from the array, since it's slightly faster.
I also removed the if statement to check if the array has more than one elements and needs to be shuffled, since with and without the if statement the code runs pretty much with the same execution time. I just removed it to make it easier to understand and the code more readable:
if(count($keyPool[$value]) > 1)
shuffle($keyPool[$value]);
You can also make some optimization changes if you want:
Preemptively return, if you get an empty array, e.g.
function randomize_duplicate_array_value_keys(array $arr){
if(empty($arr))
return [];
$uniqueValues = array_flip($arr);
$result = [];
//***
}
Preemptively return the array, if it doesn't have duplicate values:
function randomize_duplicate_array_value_keys(array $arr){
if(empty($arr))
return [];
elseif(empty(array_filter(array_count_values($arr), function($v){return $v > 1;})))
return [];
$uniqueValues = array_flip($arr);
$result = [];
//***
}
Here's another way that iterates through the sorted array while keeping track of the previous value. If the previous value is different than the current one, then the previous value is added to a new array while the current value becomes the previous value. If the current value is the same as the previous value, then depending on the outcome of rand(0,1) either the previous value is added to the new list as before, or the current value is added to the new list first:
<?php
$l = ['a' => 1,'b' => 2, 'c' => 2,
'd' => 4,'e' => 5,'f' => 5];
asort($l);
$prevK = key($l);
$prevV = array_shift($l); //initialize prev to 1st element
$shuffled = [];
foreach($l as $k => $v) {
if($v != $prevV || rand(0,1)) {
$shuffled[$prevK] = $prevV;
$prevK = $k;
$prevV = $v;
}
else {
$shuffled[$k] = $v;
}
}
$shuffled[$prevK] = $prevV;
print_r($shuffled);

Find highest value in multidimensional array [duplicate]

This question already has answers here:
Get min and max value in PHP Array
(9 answers)
Closed 2 years ago.
The Problem
I have a multidimensional array similar to the one below. What I'm trying to achieve is a way to find and retrieve from the array the one with the highest "Total" value, now I know there's a function called max but that doesn't work with a multidimensional array like this.
What I've thought about doing is creating a foreach loop and building a new array with only the totals, then using max to find the max value, which would work, the only issue would then be retrieving the rest of the data which relates to that max value. I'm not sure that's the most efficient way either.
Any ideas?
Array
(
[0] => Array
(
[Key1] => Key1
[Total] => 13
)
[1] => Array
(
[Key2] => Key2
[Total] => 117
)
[2] => Array
(
[Key3] => Key3
[Total] => 39
)
)
Since PHP 5.5 you can use array_column to get an array of values for specific key, and max it.
max(array_column($array, 'Total'))
Just do a simple loop and compare values or use array_reduce. # is an error suppressor; it hides the fact that $a['total'] is not declared before it is accessed on the first iteration. Demo
$data = array_reduce($data, function ($a, $b) {
return #$a['Total'] > $b['Total'] ? $a : $b ;
});
print_r($data);
// Array( [Key2] => Key2 [Total] => 117 )
It could also be written with arrow function syntax which has been avaiable since PHP7.4. Demo
var_export(
array_reduce(
$data,
fn($result, $row) =>
$result['Total'] > $row['Total']
? $result
: $row,
['Key1' => null, 'Total' => PHP_INT_MIN]
)
);
// array ('Key2' => 'Key2', 'Total' => 117,)
It's so basic algorithm.
$max = -9999999; //will hold max val
$found_item = null; //will hold item with max val;
foreach($arr as $k=>$v)
{
if($v['Total']>$max)
{
$max = $v['Total'];
$found_item = $v;
}
}
echo "max value is $max";
print_r($found_item);
Working demo
I know this question is old, but I'm providing the following answer in response to another question that pointed here after being marked as a duplicate. This is another alternative I don't see mentioned in the current answers.
I know there's a function called max but that doesn't work with a multidimensional array like this.
You can get around that with array_column which makes getting the maximum value very easy:
$arr = [['message_id' => 1,
'points' => 3],
['message_id' => 2,
'points' => 2],
['message_id' => 3,
'points' => 2]];
// max value
$max = max(array_column($arr, 'points'));
Getting the associative key is where it gets a little more tricky, considering that you might actually want multiple keys (if $max matches more than one value). You can do this with an anonymous function inside array_map, and use array_filter to remove the null values:
// keys of max value
$keys = array_filter(array_map(function ($arr) use ($max) {
return $arr['points'] == $max ? $arr['message_id'] : null;
}, $arr));
Output:
array(1) {
[0]=>
int(1)
}
If you do end up with multiples keys but are only interested in the first match found, then simply reference $keys[0].
another simple method will be
$max = array_map( function( $arr ) {
global $last;
return (int)( ( $arr["Total"] > $last ) ? $arr["Total"] : $last );
}, $array );
print_r( max( $max ) );
<?php
$myarray = array(
0 => array(
'Key1' => 'Key1',
'Total' => 13,
),
1 => array(
'Key2' => 'Key2',
'Total' => 117,
),
2 => array(
'Key2' => 'Key3',
'Total' => 39,
),
);
$out = array();
foreach ($myarray as $item) {
$out[] = $item['Total'];
}
echo max($out); //117
unset($out, $item);
Can be done using array_walk(array_walk_recursive if needed)
$arr is the array you want to search in
$largestElement = null;
array_walk($arr, function(&$item, $key) use (&$largestElement) {
if (!is_array($largestElement) || $largestElement["Total"] < $item["Total"]) {
$largestElement = $item;
}
});
You can use php usort function:
http://php.net/manual/en/function.usort.php
A pretty illustrative example is given here:
<?php
function cmp($a, $b)
{
return strcmp($a["fruit"], $b["fruit"]);
}
$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";
usort($fruits, "cmp");
while (list($key, $value) = each($fruits)) {
echo "\$fruits[$key]: " . $value["fruit"] . "\n";
}
?>
So it will sort the max value to the last array index.
Output:
$fruits[0]: apples
$fruits[1]: grapes
$fruits[2]: lemons
This example is given on aforementioned link
array_reduce accepts a 3rd "initial" parameter. Use this to avoid the bad practice of using "#" error suppression :
$data = array_reduce($data, function ($a, $b) {
return $a['Total'] > $b['Total'] ? $a : $b ;
},['Total' => 0]);
print_r($data);
PHP 7.4
$data = array_reduce($data, fn(a,b) => $a['Total'] > $b['Total'] ? $a : $b, ['Total' => 0]);

PHP select array element by ["something_*"]

I was wondering about this kind of situation.
What if i have a huge array say about 50k items or more.
Now let's say many of that array keys have prefix let's name it settings_, now if i want to select all values where key begins by settings_ would i need to loop trough all 50k items or is there a better way?
And say there is some "magical" way to do this with single level arrays, what about multidimensional ones?
There is preg_grep, which matches array values. Since you want to search keys, you need to invert keys and values with array_flip:
<?php
$array = array(
'armenia' => 0,
'argentina' => 1,
'brazil' => 2,
'bolivia' => 3,
'congo' => 4,
'denmark' => 5
);
$filtered = array_flip(preg_grep('/^b/', array_flip($array)));
var_dump($filtered);
/*
Output:
array(2) {
["brazil"]=>
int(2)
["bolivia"]=>
int(3)
}
*/
$arr_main_array = array('something_test' => 123, 'other_test' => 456, 'something_result' => 789);
foreach($arr_main_array as $key => $value){
$exp_key = explode('_', $key);
if($exp_key[0] == 'something'){
$arr_result[] = $value;
}
}
if(isset($arr_result)){
print_r($arr_result);
}
You can execute the code at
http://sandbox.onlinephpfunctions.com/code/884816dd115b3ccc610e1732e9716471a7b29b0f

Categories