PHP Looping based on date - php

ok so i have this array of about 151 elements and there is a date field as one of the elements. The array is a two week range. I want to count how many elements are in the first week and how any are in the second week. Here is my example array.
[0] => Array
(
[0] => 4d50
[date] => 07-10-2010
[telephone] => something
[Sno] => 1
)
[1] => Array
(
[0] => 4g50
[date] => 07-03-2010
[telephone] => something
[Sno] => 1
)
[2] => Array
(
[0] => 4s50
[date] => 06-29-2010
[telephone] => something
[Sno] => 1

function getweek($a){
//altered code., m-d-Y is no a format strtotime likes):
return DateTime::createFromFormat('m-d-Y',$a['date'])->format('W');
}
var_dump(array_count_values(array_map('getweek',$inputarray)));

Related

How to count unique values in array and store in another array PHP?

I am trying to gather together unique values in an array to be able to pass them to a new array so I can use the data to create a chart.
The data I have is as below:
Array
(
[0] => Array
(
[date] => 2020-09-17
[device] => 2
[time] => 15:51:37
[rfid] => 23641
)
[1] => Array
(
[date] => 2020-09-17
[device] => 2
[time] => 15:52:20
[rfid] => 5609
)
[2] => Array
(
[date] => 2020-09-17
[device] => 2
[time] => 15:53:23
[rfid] => 5609
)
[3] => Array
(
[date] => 2020-09-17
[device] => 2
[time] => 16:02:44
[rfid] => 5609
)
)
What would I need to do to get an output such as the following:
[date]=> 2020-09-17
[device]=>2
[RFID]=> 5609 [Count]=> 3
[RFID]=> 23641[Count]=> 1
Or is this even possible?
I changed the array from associative to an indexed array and then looped through to count the unique keys, and then add the count if the key had already been set.
$arrLength=count($count);
$elementCount=array();
for($i=0;$i<=$arrLength-1;$i++){
$key=$count[$i];
if(isset($elementCount[$key])){
$elementCount[$key]++;
} else {
$elementCount[$key]=1;
}
}
print_r($elementCount);
echo'</pre>';
Output as follows
Array
(
[2020-09-17] => 4
[15:51:37] => 1
[2] => 4
[23641] => 1
[15:52:20] => 1
[5609] => 3
[15:53:23] => 1
[16:02:44] => 1
)
This post helped :
How to remove duplicate values from an array in PHP

PHP Combine multiple arrays of objects and change array structure

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']);
}
}

Remove duplicate yyyy/mm from array

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;
}

Formatting dates in an array

I have an array which has changing key depending on the table and it can also have custom fields added. I want to be able to identify if the array contains any values that are dates and then format the date.
I have used two examples here, but they keys could be anything and the dates could be including date and/or time. The language I am using in my app is PHP5.5.
Any ideas?
Array
([0] => Array (
[id] => 1
[filename] => 1051404995566.png
[createdby] => 1
[createddate] => 2014-07-10 13:32:46
[enddate] => 2014-07-10
)
[1] => Array (
[id] => 2
[filename] => 1561404995587.png
[createdby] => 1
[createddate] => 2014-07-10 13:33:07
[enddate] => 2014-08-01
)
)

PHP: Accessing child arrays when you don't know the parent's keys and you can't just search for the value needed

I have a PHP array containing movie descriptions ($descriptions) and another PHP array containing showtimes and ticket quantities ($stock) for those movies. For each unique movie ID in $descripctions, I need to get all of the showtimes and tickets from $stock.
Is there no built-in PHP function that will search for the unique ID and return the chain of keys needed to access that value? I can't find an elegant solution after a full day of Googling and reviewing every array function in the PHP manual twice.
If there's no more elegant solution, is my best approach a custom recursive function? If so, any guidance on best approach / control structure? Maybe counting array elements and using several levels of while loops to go through each level of the array?
Here's the beginning of the $stock array I'm trying to get the data from -- won't post the entire print_r() because the source array is over 67,000 lines long but this much should indicate the structure I'm working with.
Thank you very much!!!
Array (
[products] => Array (
[venue] => Array (
[0] => Array (
[title] => 3-D Digital Cinema
[length] => 25
[memberonly] => 0
[shows] => Array (
[show] => Array (
[0] => Array (
[title] => Planet You
[location] => 3-D Digital Cinema
[eventnumber] => 521
[date] => Array (
[0] => Array (
[eventdate] => 2012/01/01
[times] => Array (
[time] => Array (
[0] => Array (
[starttime] => 13:00:00
[instock] => 110
)
[1] => Array (
[starttime] => 16:00:00
[instock] => 110
)
)
)
)
[1] => Array (
[eventdate] => 2012/01/02
[times] => Array (
[time] => Array (
[0] => Array (
[starttime] => 13:00:00
[instock] => 110
)
[1] => Array (
[starttime] => 16:00:00
[instock] => 110
)
)
)
)

Categories