I want to group results by month, to be precise Sum of total prices for each month, but i don't understand how i can divide months, then Sum price for each month.
This is required for morris chart like this
$data2[] = array(
'y' => $month,
'a' => price,
'b' => ''
);
I was using before query to get this results and was a easy way to do it, but now with code is different story.
Any help with explanation of how each segment of code is work will be so nice, so i can avoid problems like this one in future.
I need something like this :
$month = [May, June];
$price = [Sum of all prices for May, Sum of all prices for June];
This is a code :
$monthsArray = array();
// get all dates in array
foreach ($array as $key) {
// convert dates to short format, month-year -> Jun-18
$monthsArray[] = date('M-y', strtotime($key['time']));
}
$months = array();
foreach ($monthsArray as $date) {
$mon = substr($date, 0, 6);
if (!in_array($mon, $months)) array_push($months, $mon);
}
foreach ($months as $m) {
// final array for chart
$data2[] = array('y'=>$m, 'a'=>'', 'b'=>'');
}
$morris = json_encode($data2);
This is array i have
Array
(
[0] => Array
(
[price] => -1835.25
[time] => 2018-05-29 16:53:38
)
[1] => Array
(
[price] => -1743.52
[time] => 2018-05-29 16:53:39
)
[2] => Array
(
[price] => -4445.55
[time] => 2018-05-31 13:21:04
)
[3] => Array
(
[price] => -34647.04
[time] => 2018-05-31 18:29:43
)
[4] => Array
(
[price] => 16888.41
[time] => 2018-06-01 15:05:24
)
[5] => Array
(
[price] => 14369.05
[time] => 2018-06-07 14:44:21
)
[6] => Array
(
[price] => -49579.69
[time] => 2018-06-11 09:14:42
)
[7] => Array
(
[price] => -33300.94
[time] => 2018-06-08 23:50:29
)
[8] => Array
(
[price] => 4413.21
[time] => 2018-06-12 07:15:52
)
[9] => Array
(
[price] => 2724.69
[time] => 2018-06-12 07:15:46
)
[10] => Array
(
[price] => 10224.03
[time] => 2018-06-08 14:00:13
)
[11] => Array
(
[price] => -797.92
[time] => 2018-06-08 13:54:08
)
[12] => Array
(
[price] => -25157.34
[time] => 2018-06-11 11:31:31
)
[13] => Array
(
[price] => 2701.6
[time] => 2018-06-11 14:32:08
)
[14] => Array
(
[price] => 2038.92
[time] => 2018-06-12 07:15:48
)
[15] => Array
(
[price] => -10541.58
[time] => 2018-06-15 10:35:58
)
)
EDIT:
I manage to group months as you can see in updated code and now i am getting only 2 results as i should be, now its just a issue with SUM of data for each month.
This is what i am getting in console
0: {y: "May-18", a: "", b: ""}
1: {y: "Jun-18", a: "", b: ""}
Related
How I can remove duplicate entries based on Year and Month when my date format is YYYY-MM-DD? I tried removing days, but then I need to add the last day in the array, so my approach was wrong.
My array looks like this:
Array
(
[0] => Array
(
[id] => 9240399
[time] => 2018-01-01
[pages_indexed] => 942
)
[1] => Array
(
[id] => 9240322
[time] => 2018-01-02
[pages_indexed] => 940
)
[2] => Array
(
[id] => 9240344
[time] => 2018-01-03
[pages_indexed] => 947
)
[30] => Array
(
[id] => 9240344
[time] => 2018-01-31
[pages_indexed] => 947
)
[31] => Array
(
[id] => 9240344
[time] => 2018-02-01
[pages_indexed] => 1999
)
[32] => Array
(
[id] => 9240344
[time] => 2018-02-02
[pages_indexed] => 13339
)
Notice that I skipped some entries, so my dates are 2018-01-01, 2018-01-02, etc.
Array_unique would not work here since the day is different.
I tried this: ( $entries['time'] is like ex: 2018-01-01. )
$remove = DATE("Y-m",$entries['time']);
$entriesa = array_unique($remove);
$entries['time'] = $entriesa;
Well... you could loop through your results and index each key as the Year and Month, and then update this index with the row that fits the pattern, meaning you would only have the rows you expect (but you would only have the last reference of them).
Like this:
$expectedArray = [];
foreach ($arrayDuplicated as $item) {
$indexKey = substr($item['time'], 0, 7);
$expectedArray[$indexKey] = $item;
}
I want to split items which have same date and different date.
foreach ($cart->getAllItems() as $item)
{
$pickupDateTime = $item->getCartPickupDate().' '.$itemgetCartPickupTime();
$pickupDateTime = date('Y-m-d G:i:s', strtotime($pickupDateTime)) //2018-03-09 6:03:00 or 2018-03-09 21:10:00
// Split items here with same Date //
if(Dates are Same Condition){
$items_normal[]= $item->getProductId();
}else
{
//If Dates Are diffrent
$ites_special[]= $item->getProductId();
}
}
Need to compare if dates are same in first if condition and in else part the items which have different dates.
Look at the following arrays, in this I have same date and time, I need to club those which have same date and time. In below example, the 3rd one must be come under first Array as it has different time i.e. 6:05 instead of 6:03
Array
(
[1] => Array
(
[product_id] => 742
[qty] => 1
[date] => 03/09/2018
[time] => 06:03 AM
[tax] => 0
[splinst] => null
)
[2] => Array
(
[product_id] => 743
[qty] => 1
[date] => 03/09/2018
[time] => 06:03 AM
[tax] => 0
[splinst] => null
)
)
Array
(
[3] => Array
(
[product_id] => 744
[qty] => 1
[date] => 03/09/2018
[time] => 06:03 AM
[tax] => 0.12
[splinst] => null
)
[4] => Array
(
[product_id] => 757
[qty] => 1
[date] => 03/09/2018
[time] => 06:05 AM
[tax] => 0.25
[splinst] => null
)
)
You should use the DateTime object:
$pickupDateTime = $item->getCartPickupDate().' '.$itemgetCartPickupTime();
$pickupDateTime = new DateTime($pickupDateTime);
You can compare two DateTime objects which each other:
if ($dateTimeObject1 == $dateTimeObject2) {
// both have same date and time
}
Be aware that you should not use strict comparison (===) here as it would check if it is the same object and not if the DateTime is the same.
If you need to compare only the date and ignore the time you can call setTime on both objects to reset the time to 0:
$dateTimeObject1->setTime(0, 0, 0);
$dateTimeObject2->setTime(0, 0, 0);
if ($dateTimeObject1 == $dateTimeObject2) {
// both have the same date
}
You could store the date in keys into an array to group items :
foreach ($cart->getAllItems() as $item)
{
$pickupDateTime = $item->getCartPickupDate().' '.$itemgetCartPickupTime();
$pickupDateTime = date('Y-m-d G:i:s', strtotime($pickupDateTime)) //2018-03-09 6:03:00 or 2018-03-09 21:10:00
$items_special[$pickupDateTime][] = $item->getProductId();
}
foreach ($items_special as $date => $items) {
if (count($items) > 1) {
$items_normal = $items ;
unset($items_special[$date]);
break;
}
}
$items_special = array_values($items_special);
So, $items_special will look like :
Array
(
[0] => Array
(
[product_id] => 757
[qty] => 1
[date] => 03/09/2018
[time] => 06:05 AM
[tax] => 0.25
[splinst] => null
)
)
And $items_normal will look like :
Array
(
[0] => Array
(
[product_id] => 742
[qty] => 1
[date] => 03/09/2018
[time] => 06:03 AM
[tax] => 0
[splinst] => null
)
[1] => Array
(
[product_id] => 743
[qty] => 1
[date] => 03/09/2018
[time] => 06:03 AM
[tax] => 0
[splinst] => null
)
[2] => Array
(
[product_id] => 744
[qty] => 1
[date] => 03/09/2018
[time] => 06:03 AM
[tax] => 0.12
[splinst] => null
)
)
I have json return string like given below. I want to extract cancellation Policy list of objects like cutoff Time and refund In Percentage. I tried using for-loop but it didn't help me. Can you please help me on extracting this.
Array (
[apiStatus] => Array ( [success] => 1 [message] => SUCCESS ) <br>
[apiAvailableBuses] => Array ( <br>
[0] => Array ( [droppingPoints] => [availableSeats] => 41 <br>[partialCancellationAllowed] => [arrivalTime] => 08:00 AM <br>
[cancellationPolicy] => [<br>
{"cutoffTime":"1","refundInPercentage":"10"},<br>
{"cutoffTime":"2","refundInPercentage":"50"},<br>
{"cutoffTime":"4","refundInPercentage":"90"}<br>
] <br>
[boardingPoints] => Array ( [0] => Array ( [time] => 09:00PM [location] => Ameerpet,|Jeans Corner 9687452130 [id] => 6 ) [1] => Array ( [time] => 09:15PM [location] => S.R Nagar,S.R Nagar [id] => 2224 ) [2] => Array ( [time] => 09:10PM [location] => Kondapur,Toyota Show room [id] => 2244 ) ) [operatorName] => Deepak Travels [departureTime] => 9:00 PM [mTicketAllowed] => [idProofRequired] => [serviceId] => 6622 [fare] => 800 [busType] => 2+1 Hi-Tech Non A/c [routeScheduleId] => 6622 [commPCT] => 0 [operatorId] => 213 [inventoryType] => 0 ) <br>
[1] => Array ( [droppingPoints] => [availableSeats] => 41 [partialCancellationAllowed] => [arrivalTime] => 07:00 AM <br>
[cancellationPolicy] => [<br>
{"cutoffTime":"1","refundInPercentage":"10"},<br>
{"cutoffTime":"2","refundInPercentage":"50"},<br>
{"cutoffTime":"4","refundInPercentage":"90"}<br>
] <br>
[boardingPoints] => Array ( [0] => Array ( [time] => 09:10PM [location] => Ameerpet,|Jeans Corner [id] => 6 ) [1] => Array ( [time] => 09:00PM [location] => S.R Nagar,S.R Nagar [id] => 2224 ) [2] => Array ( [time] => 08:30PM [location] => KUKATPALLY,JNTU [id] => 2230 ) ) [operatorName] => Dhanunjayabus [departureTime] => 9:00 PM [mTicketAllowed] => [idProofRequired] => [serviceId] => 6743 [fare] => 900 [busType] => VOLVO [routeScheduleId] => 6743 [commPCT] => 0 [operatorId] => 233 [inventoryType] => 0 )
)
)
Use a foreach() for it like so:
foreach ($your_response['apiAvailableBuses'] as $el) {
$cancellationPolicy[] = $el['cancellationPolicy'];
}
Try this:
foreach($data['apiStatus']['apiAvailableBuses'] as $item) {
foreach($item['cancellationPolicy'] as $key => $json) {
$jsonDecoded = json_decode($json, true);
// And you will have access to the data like this
// $jsonDecoded['cutoffTime'];
// $jsonDecoded['refundInPercentage'];
}
}
$response = json_decode($apiResponse);
$cancellationPolicies = [];
foreach ($response->apiAvailableBuses as $availableBus) {
$cancellationPolicies[] = $availableBus['cancellationPolicy'];
// if you want to display something you can simply do it like this;
echo $availableBus['cancellationPolicy']->cutoffTime;
}
I been playing around with google chart for my project, and somehow I'm still cant figure out displaying multidimensional array (from database).
print_r() output:
Array
(
[0] => Array
(
[year] => 2000
[value] => 2766
[typeName] => Oil
)
[1] => Array
(
[year] => 2000
[value] => 3098
[typeName] => Gas
)
[2] => Array
(
[year] => 2000
[value] => 269814
[typeName] => Coal
)
[3] => Array
(
[year] => 1999
[value] => 2836
[typeName] => Oil
)
[4] => Array
(
[year] => 1999
[value] => 3150
[typeName] => Gas
)
[5] => Array
(
[year] => 1999
[value] => 257561
[typeName] => Coal
)
);
as google chart required data format, I want it to be something like this:
['Year' , 'Oil', 'Gas', 'Coal']
["2000", 2766, 3098, 269814],
["1999" 2836, 3150, 257561]
Can someone help me how to play around with the multidimensional?
Thanks!
You shuld convert this data into json. Just parse in this way to get the desired structure O(n)
$headers = array("year" => 0, "oil" => 1, "gas" => 2, "coal" => 3);
$response[] = array_map("ucfirst",array_keys($headers));
foreach($orig_array as $k => $item) {
$temp_data[$item['year']][0] = (string) $item['year'];
$temp_data[$item['year']][$headers[$item['typeName']]] = $item['value'];
if(4==count($temp_data[$item['year']])) $response[] = $temp_data[$item['year']];
}
$json_data = json_encode($response);
echo($json_data);
I have the following code:
<?php
//The company_array:
$company_array = array(
"AAA" => "AAA",
"BBB" => "BBB",
"CCC" => "CCC",
"DDD" => "DDD"
);
$platform_data = 'PC'; //Just to keep it short :)
foreach ($company_array as $company) {
if ($stmt = $mysqli->prepare("SELECT price, time FROM $company WHERE platform = ? ORDER BY time ASC")) {
$stmt->bind_param("s", $platform_data);
$stmt->execute();
$stmt->bind_result($price[$company], $time[$company]);
$i=0;
while ($stmt->fetch()) {
$company_info[$company][$i] = array('Price' => $price[$company], 'Time' => $time[$company]);
$i++;
}
$stmt->close();
}
?>
Now I had some issues with this, the last iteration of the loop seems to break, if I print $company_info all but the last company displays fine, the last one seem to repeat the last value for all the rows:
Array
(
[AAA] => Array
(
[0] => Array
(
[Price] => 626.8600
[Time] => 2013-09-27 14:30:06
)
[1] => Array
(
[Price] => 615.5900
[Time] => 2013-09-27 15:45:05
)
[2] => Array
(
[Price] => 604.7400
[Time] => 2013-09-27 17:45:05
)
)
[BBB] => Array
(
[0] => Array
(
[Price] => 246.7200
[Time] => 2013-09-27 14:30:06
)
[1] => Array
(
[Price] => 245.4700
[Time] => 2013-09-27 15:45:05
)
[2] => Array
(
[Price] => 244.8300
[Time] => 2013-09-27 17:45:05
)
)
[CCC] => Array
(
[0] => Array
(
[Price] => 189.0900
[Time] => 2013-09-27 14:30:06
)
[1] => Array
(
[Price] => 188.9800
[Time] => 2013-09-27 15:45:05
)
[2] => Array
(
[Price] => 188.8900
[Time] => 2013-09-27 17:45:05
)
)
[DDD] => Array
(
[0] => Array
(
[Price] => 134.3100
[Time] => 2013-10-06 13:30:06
)
[1] => Array
(
[Price] => 134.3100
[Time] => 2013-10-06 13:30:06
)
[2] => Array
(
[Price] => 134.3100
[Time] => 2013-10-06 13:30:06
)
)
)
As you can see company AAA, BBB and CCC all have different Prices and Times in each of their arrays, but company DDD has the same value repeated 3 times (the last value in the database) when the values should be different in the same way as the other companies.
Now from what I have read I am doing this wrong, instead of using foreach I should use implode() on the array and use that, but its confusing me since I can not find a good example where it is used on the FROM field (seems to only be used on WHERE)
My question would be, how can I get away from using a foreach loop since it seems to be causing issues, and use the proper implode() method on the FROM field?
I am guessing I need to modify my while loop also so that the output remains in the same format.