Cumulative sum in PHP Array from MySQL Query - php

I want to do a cumulative sum of data from MySQL query. Already my SQL query working well but i don't know how to do a cumulative sum of column 'suma'. I want to stay with dates from MySQL query table but it should look like this that next index will have cumulative sum of this index and previous.
My array from MySQL query look like this.
[0] => Array
(
[data] => 2017-11-01
[SUMA] => 19
)
[1] => Array
(
[data] => 2017-11-02
[SUMA] => 97
)
[2] => Array
(
[data] => 2017-11-03
[SUMA] => 296
)
[3] => Array
(
[data] => 2017-11-05
[SUMA] => 58
)
[4] => Array
(
[data] => 2017-11-06
[SUMA] => 216
)
[5] => Array
(
[data] => 2017-11-07
[SUMA] => 194
)
[6] => Array
(
[data] => 2017-11-08
[SUMA] => 444
)
[7] => Array
(
[data] => 2017-11-09
[SUMA] => 301
)
[8] => Array
(
[data] => 2017-11-10
[SUMA] => 213
)
[9] => Array
(
[data] => 2017-11-11
[SUMA] => 5
)
[10] => Array
(
[data] => 2017-11-12
[SUMA] => 8
)
[11] => Array
(
[data] => 2017-11-13
[SUMA] => 186
)
[12] => Array
(
[data] => 2017-11-14
[SUMA] => 227
)
[13] => Array
(
[data] => 2017-11-15
[SUMA] => 180
)
[14] => Array
(
[data] => 2017-11-16
[SUMA] => 225
)
)
And I want to this array or new one look like this:
[0] => Array
(
[data] => 2017-11-01
[SUMA] => 19
)
[1] => Array
(
[data] => 2017-11-02
[SUMA] => 116
)
[2] => Array
(
[data] => 2017-11-03
[SUMA] => 412
)
[3] => Array
(
[data] => 2017-11-05
[SUMA] => 470
)
[4] => Array
(
[data] => 2017-11-06
[SUMA] => 686
)
[5] => Array
(
[data] => 2017-11-07
[SUMA] => 880
)
[6] => Array
(
[data] => 2017-11-08
[SUMA] => 1324
)
[7] => Array
(
[data] => 2017-11-09
[SUMA] => 1625
)
[8] => Array
(
[data] => 2017-11-10
[SUMA] => 1838
)
[9] => Array
(
[data] => 2017-11-11
[SUMA] => 1843
)
[10] => Array
(
[data] => 2017-11-12
[SUMA] => 1851
)
[11] => Array
(
[data] => 2017-11-13
[SUMA] => 2037
)
[12] => Array
(
[data] => 2017-11-14
[SUMA] => 2264
)
[13] => Array
(
[data] => 2017-11-15
[SUMA] => 2444
)
[14] => Array
(
[data] => 2017-11-16
[SUMA] => 2699
)
I tried like this but it's only showing me last date and full cumulative instead of each index.
while ($row = mysqli_fetch_assoc($result)) {
$myArray['data'] = $row['data'];
$myArray['suma'] += $row['SUMA'];
}

Do it while creating array
$s=0;
$myArray = [];
while ($row = mysqli_fetch_assoc($result)) {
$temp['data'] = $row['data'];
$s = $temp['suma'] = $row['SUMA'] + $s;
$myArray[] = $temp;
}
echo '<pre>';
print_r($myArray);

<?php
$array=array(
0 => Array
(
'data' => '2017-11-01',
'SUMA' => 19
),
1 => Array
(
'data' => '2017-11-02',
'SUMA' => 97
),
2 => Array
(
'data' => '2017-11-03',
'SUMA' => 296,
),
3 => Array
(
'data' => '2017-11-05',
'SUMA' => 58,
),
);
$i=0;
while ($i<count($array) )
{
if($i==0) {
$newArray[0]['data'] = $array[$i]['data'];
$newArray[0]['SUMA'] = $array[$i]['SUMA'];
}
else{
$newArray[$i]['data']=$array[$i]['data'];
$newArray[$i]['SUMA']=$array[$i]['SUMA']+$newArray[$i-1]['SUMA'];
}
$i++;
}
echo '<pre>';
print_r($newArray);
and the output is :
Array
(
[0] => Array
(
[data] => 2017-11-01
[SUMA] => 19
)
[1] => Array
(
[data] => 2017-11-02
[SUMA] => 116
)
[2] => Array
(
[data] => 2017-11-03
[SUMA] => 412
)
[3] => Array
(
[data] => 2017-11-05
[SUMA] => 470
)
)
You can remove the if/else and keep only the else but you get a notice for position -1 of the array. The code is working fine you just have to disable the notice warning (i don't recommend it do it only if you are 100% sure)

Related

Rearrange items in multidimensional array

I have a multidimensional array, I want to take the key 'data' from each array item and form another array, like first array 'data' element has 3 items, second has 1 item, 3rd and 4th are empty, and 5th has one item, I want to make a separate array like $temp_array['first_item_from_first_array,..., first_item_from_fifth_array, 'second_item_from_second_array,....,second_item_From_fifth_array]
input array is,
Array
(
[0] => Array
(
[meal_type] => bf
[label] => Breakfast
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[2] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
)
)
[1] => Array
(
[meal_type] => sn
[label] => Snacks
[calorie_limit] => 10
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
)
)
[2] => Array
(
[meal_type] => lu
[label] => Lunch
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
)
)
[3] => Array
(
[meal_type] => su
[label] => Supper
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
)
)
[4] => Array
(
[meal_type] => dn
[label] => Dinner
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)
)
)
the output to be like this
Array
(
[0] => Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)
[1] => Array
(
[0] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
)
[2] => Array
(
[0] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
)
)
You need to do it like below:-
$data_array= array_column($array,'data');
$count = 0;
foreach($data_array as $data){
$real_count = count($data);
if($real_count > $count){
$count = $real_count;
}
}
echo $count;
$final_array = [];
foreach($data_array as $data_arr){
for($i=0;$i< $count; $i++){
$final_array[$i][] = (count($data_arr[$i])>0)? $data_arr[$i]: array();
}
}
echo "<pre/>";print_r($final_array);
Output:-https://eval.in/925383
Reference:- PHP: array_column - Manual
You can use array_column() function
Example :-
<?php
$array = [
[
"meal_type" => "bf",
"data" => [
"name" => "test",
"email" => "test#ymail.com"
]
],
[
"meal_type" => "bf",
"data" => []
]
];
echo "<pre>";
print_r(array_column($array, "data"));
?>
You can do this using foreach and array_push.
$data_item_array = [];
foreach ($array as $item) {
$data = $item['data'];
foreach ($data as $t) {
array_push($data_item_array, $t);
}
}
print_r($data_item_array);
Output will be :
Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[2] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
[3] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
[4] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)

Looping a Complex Multidimensional Array

I'm trying to extract all the variables from a complex array of nutrition results from Neutronix API.
The array is as follows:
Food Array:
Array ( [foods] =>
Array ( [0] => Array (
[food_name] => kale
[brand_name] =>
[serving_qty] => 1
[serving_unit] => cup, chopped
[serving_weight_grams] => 130
[nf_calories] => 36.4
[nf_total_fat] => 0.52
[nf_saturated_fat] => 0.07
[nf_cholesterol] => 0
[nf_sodium] => 29.9
[nf_total_carbohydrate] => 7.32
[nf_dietary_fiber] => 2.6
[nf_sugars] => 1.63
[nf_protein] => 2.47
[nf_potassium] => 296.4
[nf_p] => 36.4
[full_nutrients] => Array (
[0] => Array (
[attr_id] => 203
[value] => 2.47
)
[1] => Array (
[attr_id] => 204
[value] => 0.52
)
[2] => Array (
[attr_id] => 205
[value] => 7.319
)
[3] => Array (
[attr_id] => 207
[value] => 1.131
)
[4] => Array (
[attr_id] => 208
[value] => 36.4
)
[5] => Array (
[attr_id] => 221
[value] => 0
)
[6] => Array (
[attr_id] => 255
[value] => 118.56
)
[7] => Array (
[attr_id] => 262
[value] => 0
)
[8] => Array (
[attr_id] => 263
[value] => 0
)
[9] => Array (
[attr_id] => 268
[value] => 152.1
)
[10] => Array (
[attr_id] => 269
[value] => 1.625
)
[11] => Array (
[attr_id] => 291
[value] => 2.6
)
[12] => Array (
[attr_id] => 301
[value] => 93.6
)
[13] => Array (
[attr_id] => 303
[value] => 1.17
)
[14] => Array (
[attr_id] => 304
[value] => 23.4
)
[15] => Array (
[attr_id] => 305
[value] => 36.4
)
[16] => Array (
[attr_id] => 306
[value] => 296.4
)
[17] => Array (
[attr_id] => 307
[value] => 29.9
)
[18] => Array (
[attr_id] => 309
[value] => 0.312
)
[19] => Array (
[attr_id] => 312
[value] => 0.2028
)
[20] => Array (
[attr_id] => 315
[value] => 0.5408
)
[21] => Array (
[attr_id] => 317
[value] => 1.17
)
)
[nix_brand_name] =>
[nix_brand_id] =>
[nix_item_name] =>
[nix_item_id] =>
[upc] =>
[consumed_at] => 2017-09-08T22:44:54+00:00
[metadata] => Array ( )
[source] => 1
[ndb_no] => 11234
[tags] => Array (
[item] => kale
[measure] =>
[quantity] => 1.0
[tag_id] => 644
)
[alt_measures] => Array (
[0] => Array (
[serving_weight] => 130
[measure] => cup, chopped
[seq] => 1
[qty] => 1 )
[1] => Array (
[serving_weight] => 130
[measure] => cup
[seq] => 80
[qty] => 1 )
[2] => Array (
[serving_weight] => 2.71
[measure] => tsp
[seq] => 101
[qty] => 1 )
[3] => Array (
[serving_weight] => 8.13
[measure] => tbsp
[seq] => 102
[qty] => 1 )
)
[lat] =>
[lng] =>
[meal_type] => 5
[photo] => Array (
[thumb] => https://d2xdmhkmkbyw75.cloudfront.net/644_thumb.jpg
[highres] => https://d2xdmhkmkbyw75.cloudfront.net/644_highres.jpg
)
[sub_recipe] =>
)
)
)
My PHP Code so far is below. I've inserted comments where I am stuck as to how to get the variables.
I'm also not sure if I'm looping the segments of the array correctly.
foreach ($foods as $food){
foreach($food as $key => $val) {
//get values for each variable
foreach($full_nutrients as $nutrient){
foreach($nutrient as $key => $val){
//get values for each variable
}
}
foreach($metadata as $meta){
$source = $meta['source'];
$ndb_no = $meta['ndb_no'];
foreach($tags as $tag){
$tag_item = $tag['item'];
$tag_measure = $tag['measure'];
$tag_quantity = $tag['quantity'];
$tag_id = $tag['tag_id'];
}
}
foreach($alt_measures as $alt_meaasure){
foreach($alt_meaasure as $key => $val){
//get alt_measures
}
}
foreach($photo as $image){
$image_thumb = $image['thumb'];
$image_highres = $image['highres'];
}
}
}

Array Formation - PHP

I am running an SQL Query in order to get the post IDs of the user currently logged in.
My query works as designed.
$userGadgets = $wpdb->get_results("SELECT item_id FROM mg_gd_mylist WHERE user_id = '$user_id'", ARRAY_A);
The issue I am facing has to do with the array returned.
Array
(
[0] => Array
(
[item_id] => 10318
)
[1] => Array
(
[item_id] => 10378
)
[2] => Array
(
[item_id] => 10566
)
[3] => Array
(
[item_id] => 10608
)
[4] => Array
(
[item_id] => 10614
)
[5] => Array
(
[item_id] => 10648
)
[6] => Array
(
[item_id] => 10292
)
[7] => Array
(
[item_id] => 10274
)
[8] => Array
(
[item_id] => 10306
)
[9] => Array
(
[item_id] => 9312
)
[10] => Array
(
[item_id] => 10652
)
[11] => Array
(
[item_id] => 10324
)
[12] => Array
(
[item_id] => 10342
)
[13] => Array
(
[item_id] => 10696
)
[14] => Array
(
[item_id] => 10672
)
[15] => Array
(
[item_id] => 10372
)
)
The model I am trying to produce is the following
Array
(
[0] => 140 //The item_id string as an integer
[1] => 141
[2] => 142
)
Any ideas on that one ? Thank you in advance.
check this code , guess you want to single array result
check array_reduce () built in function
<?php
$your_array = array(0 => array('item_id' => 3160), 1 => array('item_id' => 13123), 2 => array('item_id' => 234234), 3 => array('item_id' => 2123));
echo "<pre>";
print_r($your_array);
$convert_array = array_map('intval', array_column($your_array, 'item_id'));
echo "<pre>";
print_r($convert_array);
then your original Array :
Array
(
[0] => Array
(
[item_id] => 3160
)
[1] => Array
(
[item_id] => 13123
)
[2] => Array
(
[item_id] => 234234
)
[3] => Array
(
[item_id] => 2123
)
)
and output :
Array
(
[0] => 3160
[1] => 13123
[2] => 234234
[3] => 2123
)
Why would that be a bad output?
Simply use:
array = [];
foreach($arrayReturned as $i=>$row){
// you get every data with $row['item_id'], for example
array[] = $row['item_id'];
}

echo multidimensional array values into a readable format

I have a crazy array from google analytics API:
print_r($visits);
Produces the following:
Array ( [http_code] => 200 [kind] => analytics#gaData [id] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:615743&dimensions=ga:date&metrics=ga:visits&start-date=2015-07-07&end-date=2015-08-07 [query] => Array ( [start-date] => 2015-07-07 [end-date] => 2015-08-07 [ids] => ga:615743 [dimensions] => ga:date [metrics] => Array ( [0] => ga:visits ) [start-index] => 1 [max-results] => 1000 ) [itemsPerPage] => 1000 [totalResults] => 32 [selfLink] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:615743&dimensions=ga:date&metrics=ga:visits&start-date=2015-07-07&end-date=2015-08-07 [profileInfo] => Array ( [profileId] => 615743 [accountId] => 391435 [webPropertyId] => UA-391435-1 [internalWebPropertyId] => 642064 [profileName] => www.website.co.uk [tableId] => ga:615743 ) [containsSampledData] => [columnHeaders] => Array ( [0] => Array ( [name] => ga:date [columnType] => DIMENSION [dataType] => STRING ) [1] => Array ( [name] => ga:visits [columnType] => METRIC [dataType] => INTEGER ) ) [totalsForAllResults] => Array ( [ga:visits] => 8250 ) [rows] => Array ( [0] => Array ( [0] => 20150707 [1] => 271 ) [1] => Array ( [0] => 20150708 [1] => 266 ) [2] => Array ( [0] => 20150709 [1] => 251 ) [3] => Array ( [0] => 20150710 [1] => 264 ) [4] => Array ( [0] => 20150711 [1] => 351 ) [5] => Array ( [0] => 20150712 [1] => 244 ) [6] => Array ( [0] => 20150713 [1] => 309 ) [7] => Array ( [0] => 20150714 [1] => 250 ) [8] => Array ( [0] => 20150715 [1] => 277 ) [9] => Array ( [0] => 20150716 [1] => 214 ) [10] => Array ( [0] => 20150717 [1] => 215 ) [11] => Array ( [0] => 20150718 [1] => 167 ) [12] => Array ( [0] => 20150719 [1] => 228 ) [13] => Array ( [0] => 20150720 [1] => 290 ) [14] => Array ( [0] => 20150721 [1] => 236 ) [15] => Array ( [0] => 20150722 [1] => 245 ) [16] => Array ( [0] => 20150723 [1] => 267 ) [17] => Array ( [0] => 20150724 [1] => 307 ) [18] => Array ( [0] => 20150725 [1] => 271 ) [19] => Array ( [0] => 20150726 [1] => 226 ) [20] => Array ( [0] => 20150727 [1] => 319 ) [21] => Array ( [0] => 20150728 [1] => 299 ) [22] => Array ( [0] => 20150729 [1] => 263 ) [23] => Array ( [0] => 20150730 [1] => 242 ) [24] => Array ( [0] => 20150731 [1] => 233 ) [25] => Array ( [0] => 20150801 [1] => 165 ) [26] => Array ( [0] => 20150802 [1] => 170 ) [27] => Array ( [0] => 20150803 [1] => 349 ) [28] => Array ( [0] => 20150804 [1] => 410 ) [29] => Array ( [0] => 20150805 [1] => 282 ) [30] => Array ( [0] => 20150806 [1] => 256 ) [31] => Array ( [0] => 20150807 [1] => 113 ) ) )
If I replace print_r($visits); with
foreach ($visits as $key => $val) {
echo $val;
}
I get the following which is more readable:
200analytics#gaDatahttps://www.googleapis.com/analytics/v3/data/ga?ids=ga:615743&dimensions=ga:date&metrics=ga:visits&start-date=2015-07-07&end-date=2015-08-07Array100032https://www.googleapis.com/analytics/v3/data/ga?ids=ga:615743&dimensions=ga:date&metrics=ga:visits&start-date=2015-07-07&end-date=2015-08-07ArrayArrayArrayArray
My question is, how do I access the Arrays within this Array?
I'd ideally like to print out the entire $visits array in something readable.
If you want this for debugging then output like this:
echo '<pre>';
print_r($visits);
echo '</pre>';
Other ways use array_walk_recursive()
You can use array_walk_recursive() to loop through each value of your array, e.g.
array_walk_recursive($visits, function($v, $k){
echo $v . "<br>";
});

in_array still gets the company id even though I put a check

I have the following code for the array below
$arrCompany = array();
foreach($users as $k => $user){
if(!in_array($user['User']['company_id'], $arrCompany)){
$arrCompany['company'][] = $user['User']['company_id'];
}else{}
}
what I am trying to do is just have one entery of company id so I can add users under it but for some reasons its not working
here is my array
Array
(
[0] => Array
(
[User] => Array
(
[id] => 1
[company_id] => 20
[type] =>
)
)
[1] => Array
(
[User] => Array
(
[id] => 6
[company_id] => 21
[type] =>
)
)
[2] => Array
(
[User] => Array
(
[id] => 7
[company_id] => 22
[type] =>
)
)
[3] => Array
(
[User] => Array
(
[id] => 14
[company_id] => 21
[type] =>
)
)
[4] => Array
(
[User] => Array
(
[id] => 15
[company_id] => 22
[type] =>
)
)
[5] => Array
(
[User] => Array
(
[id] => 16
[company_id] => 21
[type] =>
)
)
)
)
when I do var_dump...i get this
Array
(
[company] => Array
(
[0] => 20
[1] => 21
[2] => 22
[3] => 21
[4] => 22
[5] => 21
)
)
you can see the company has been repeated
$arrCompany = array('company'=>array());
foreach($users as $k => $user){
if(!in_array($user['User']['company_id'], $arrCompany['company'])){
$arrCompany['company'][] = $user['User']['company_id'];
}
}

Categories