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;
}
Related
I have multiple separate arrays of objects that are imported (JSON-encoded) from MySQL that I need to merge and change structure. I basically need the combined [time] and [day] entries to be an array of [cycle] so I can loop over them. Currently after import/decoding the array of objects structure for each MySQL query looks like this:
(query 1)
stdClass Object
(
[day] => Array
(
[0] => stdClass Object
(
[day] => 1
[time] => 60
[name] => Running
[cycle] => 1
)
[1] => stdClass Object
(
[day] => 5
[time] => 30
[name] => Running
[cycle] => 1
)
)
[id] => 15359593
)
(query 2)
stdClass Object
(
[day] => Array
(
[0] => stdClass Object
(
[day] => 1
[time] => 55
[name] => Running
[cycle] => 2
)
[1] => stdClass Object
(
[day] => 5
[time] => 15
[name] => Running
[cycle] => 2
)
)
[id] => 36848901
)
The structure that need is:
stdClass Object
(
[day] => 1
[name] => Running
[cycle] => Array
(
[0] => stdClass Object
(
[time] => 60
[cycle] => 1
[day] => 1
)
[1] => stdClass Object
(
[time] => 55
[cycle] => 2
[day] => 1
)
)
[id] => 36848901
)
stdClass Object
(
[day] => 5
[name] => Running
[cycle] => Array
(
[0] => stdClass Object
(
[time] => 30
[cycle] => 1
[day] => 5
)
[1] => stdClass Object
(
[time] => 15
[cycle] => 2
[day] => 5
)
)
[id] => 1237465
)
I need the program to then iterate over the array using foreach [day] and then [cycle] to produce something like this:
name day cycle 1 cycle 2 cycle ..
Running 1 60 55 ..
Running 5 30 15 ..
..
It can only do it on a row by row basis. I don't have (much) control over this part of the process.
I have tried changing the structure by using foreach loops and array commands like this:
$newArray[] = array( "name" => $this->name, "day" => $this->day,
array("cycle" => $this->cycle, array("time" => $this->time, "cycle" =>
$this->cycle, "day" => $this->day)));
This gives me a structure that is almost right, per entry but not combined for all.
To combine them I've tried array_merge_recursive() and various variants but no luck.
So what I think I need is to merge the arrays of objects and then change the structure to have the values of each [time] and [day] to be nested inside the [cycle] so I can loop over them.
What is the best way to do this?
It is running on PHP 7.2.
More of my attempted code:
// get {data} part of JSON-encoded field for each mysql result
for ($x = 0; $x < $this->cycleCount; $x++) {
preg_match("/{.*}/",$this->tmpString[$x]['data'],$matches);
$this->data[$x] = json_decode($matches[0]);
foreach ($this->data[$x] as $day) {
$newArray[] = array( "name" => $day->name, "day" => $day->day,
array("cycle" => $day->cycle,
array("time" => $day->time,
"cycle" => $day->cycle,
"day" => $day->day)
)
);
}
$data = array();
$object = json_decode($querObject,true);
foreach($object as $day => $info)
{
foreach($info['cycle'] as $cycleInfo)
{
$data[$info['$id']]['name'] = $cycleInfo['name'];
$data[$info['$id']]['day'] = $cycleInfo['day'];
$data[$info['$id']]['id'] = $cycleInfo['id'];
$data[$info['$id']]['cycle'][] =array('time'=>$cycleInfo['time'],'cycle'=>$cycleInfo['cycle'],'day'=>=>$cycleInfo['day']);
}
}
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: ""}
I have following 'challange';
I have array like this:
Array
(
[0] => stdClass Object
(
[id] => 94
[day] => Monday
[date] => 2018-07-09
[week_number] => 2
)
[1] => stdClass Object
(
[id] => 95
[day] => Tuesday
[date] => 2018-07-10
[week_number] => 2
)
[2] => stdClass Object
(
[id] => 83
[day] => Saturday
[date] => 2018-07-07
[week_number] => 1
)
[3] => stdClass Object
(
[id] => 82
[day] => Friday
[date] => 2018-07-06
[week_number] => 1
)
[4] => stdClass Object
(
[id] => 81
[day] => Thursday
[date] => 2018-07-05
[week_number] => 1
)
[5] => stdClass Object
(
[id] => 80
[day] => Wednesday
[date] => 2018-07-04
[week_number] => 1
)
)
I wanted to know how many times user have selected "week_number" 1,2 and so on, I don't want to allow user to select more than 3 events in a week.
I am using fullcalendar to display events.
How can I achieve that?
Thank you in advance
In PHP 7 you can extract the week_number properties and count the values:
$result = array_count_values(array_column($array, 'week_number'));
Will yield the week_number as the key and the count as the value:
array
(
[1] => 4
[2] => 2
)
Depending upon wheteher you want to check for multiples or just one, loop and check for > 3 or use in_array(3, $result).
You might use array_reduce to
$result = array_reduce($input, function($outpu, $item) {
if(!isset($output[$item->week_number])) {
$output[$item->week_number] = 0;
}
return $output[$item->week_number]++;
});
var_dump($result);
Where $input is You array of objects
If you only need to check if there are more than x events, and you don't need to count all events, better use something like this:
function hasTooManySelections($items, $maxSelections = 3)
{
$counts = [];
foreach ($items as $item) {
$counts[$item->week_number] = isset($counts[$item->week_number]) ? $counts[$item->week_number] + 1 : 1;
if ($counts[$item->week_number] > $maxSelections) {
return true;
}
}
return false;
}
var_dump(hasTooManySelections($items));
Demo: https://3v4l.org/XbiH0
You can sort your array in respect of group by week number and then count length of all perticular group
I am using PHP 5.5.12.
I have an array like:
Array
(
[0] => Array
(
[user_id] => 3
[medicine_id] => 1
[time] => Array
(
[0] => stdClass Object
(
[event_type] => before_breakfast
[time] => 07:00:00
)
[1] => stdClass Object
(
[event_type] => after_breakfast
[time] => 07:30:00
)
)
)
[1] => Array
(
[user_id] => 3
[medicine_id] => 2
[time] => Array
(
[0] => stdClass Object
(
[event_type] => before_lunch
[time] => 13:00:00
)
[1] => stdClass Object
(
[event_type] => after_lunch
[time] => 14:00:00
)
)
)
[2] => Array
(
[user_id] => 3
[medicine_id] => 3
[time] => Array
(
[0] => stdClass Object
(
[event_type] => before_dinner
[time] => 20:00:00
)
[1] => stdClass Object
(
[event_type] => after_lunch
[time] => 21:00:00
)
)
)
)
I want to json_encode() the field time of each root level.
I tried using:
foreach ($user_medicine_times as $user_medicine_key => $user_medicine_value) {
$user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}
and:
foreach ($user_medicine_times as $user_medicine_key => &$user_medicine_value) {
$user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}
But using print_r($user_medicine_value), it returns the same array.
I want the result to be as follows:
Array
(
[0] => Array
(
[user_id] => 3
[medicine_id] => 1
[time] => "[{"event_type":"before_breakfast","time":"07:00:00"},{"event_type":"after_breakfast","time":"07:30:00"}]"
)
[1] => Array
(
[user_id] => 3
[medicine_id] => 2
[time] => "[{"event_type":"before_lunch","time":"13:00:00"},{"event_type":"after_lunch","time":"17:00:00"}]"
)
[2] => Array
(
[user_id] => 3
[medicine_id] => 3
[time] => "[{"event_type":"before_dinner","time":"20:00:00"},{"event_type":"after_lunch","time":"17:00:00"}]"
)
)
How can I achieve this result?
I have read your question earlier and prepared the answer but you removed it before i paste the answer. Anyways here is the solution
function outer(&$val, $key) {
$val['time'] = json_encode($val['time']);
}
array_walk($your_array, 'outer');
print_r($your_array);
You can replace your foreach loop's content with something like this:
foreach ($user_medicine_times as $user_medicine_key => $user_medicine_value) {
$user_medicine_times[$user_medicine_key]['time'] = json_encode($user_medicine_value['time'], true);
}
Maybe the json encode fails because your time array contains an stdClass Object. Try to convert this like that :
$result = array();
foreach ($user_medicine_value['time'] as $value) {
$result['event_type'] = $value->event_type;
$result['time'] = $value->time;
}
$user_medicine_value['time'] = $result;
Because, in every iteration, the value is not being saved anywhere,
You have two options here, either make new array having time key with json_encode() or pass the value by reference as shown below.
foreach ($user_medicine_times as $user_medicine_key => &$user_medicine_value) {
^
$user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}
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.