php compare todays date with dates in (multi-dimensional) array? - php

I have an array of dates, I was wondering if it was possible to search an array and remove any items which are BEFORE todays date, and keep the rest?
Here's my array
Array
(
[0] => Array
(
[date] => 2013-07-14
)
[1] => Array
(
[date] => 2013-08-31
)
[2] => Array
(
[date] => 2013-09-15
)
[3] => Array
(
[date] => 2013-10-12
)
[4] => Array
(
[date] => 2013-10-16
)
[5] => Array
(
[date] => 2013-10-19
)
[6] => Array
(
[date] => 2013-10-23
)
[7] => Array
(
[date] => 2013-10-26
)
[8] => Array
(
[date] => 2013-10-30
)
[9] => Array
(
[date] => 2013-09-07
)
[10] => Array
(
[date] => 2013-08-14
)
[11] => Array
(
[date] => 2013-08-24
)
[12] => Array
(
[date] => 2013-09-11
)
[13] => Array
(
[date] => 2013-09-28
)
[14] => Array
(
[date] => 2013-10-05
)
)

Something like that:
$buffer = array();
foreach ($dates as $element) {
if (strtotime($element['date']) >= time()) {
$buffer[] = $element;
}
}

What about looping throgh and comparing dates with actual required data and build a new filtered array?
$newarray = array();
foraeach($array as $data)
{
if($data['date'] >= $yourdate)
{
$newarray] = $data;
}
}

array_walk function lets you define a callback to run on every item in an array.

Related

how can i get count of each age group using php

please check this :
Array
(
[0] => 46-65
[1] => 7-12|31-45
[2] => 31-45
[3] => 31-45
[4] => 66+
[5] => 18-30
[6] => 46-65
[7] => 13-17|46-65
Here i converted string to array then i got below array :
Array
(
[0] => Array
(
[0] => 46-65
)
[1] => Array
(
[0] => 7-12
[1] => 31-45
)
[2] => Array
(
[0] => 31-45
)
[3] => Array
(
[0] => 31-45
)
[4] => Array
(
[0] => 66+
)
[5] => Array
(
[0] => 18-30
)
[6] => Array
(
[0] => 46-65
)
[7] => Array
(
[0] => 13-17
[1] => 46-65
)
[8] => Array
(
[0] => 31-45
)
[9] => Array
(
[0] => 31-45
)
[10] => Array
(
[0] => 31-45
)
[11] => Array
(
[0] => 18-30
)
[12] => Array
(
[0] =>
)
[13] => Array
(
[0] => 46-65
)
[14] => Array
(
[0] => 7-12
[1] => 31-45
)
[15] => Array
(
[0] => 18-30
)
[16] => Array
(
[0] => 18-30
[1] => 31-45
)
[17] => Array
(
[0] => 18-30
)
[18] => Array
(
[0] => 31-45
)
[19] => Array
(
[0] => 18-30
)
[20] => Array
(
[0] => 13-17
[1] => 18-30
[2] => 46-65
)
}
I stored age group in string format because each row can be have multiple age group. after that i converted into array.
How can i get count of each age group like 46-65 is 6 times 18-30 is 8 times.
HEre i want count of each age group
This code should work for that:
<?php
$selections = [/** your array here */];
$counted = [];
// Loop full list
foreach($selections as $selection) {
// Loop age elements in list
foreach ($selection as $age) {
// If not already counted initialise age range
if(!isset($counted[$age])) {
$counted[$age] = 0;
}
// Increment age range
$counted[$age]++;
}
}
var_dump($counted);
I'm not sure I fully understood tom problem. Try something like this.
This will return you a array with as key the groups and as value the number of times it has been met.
I used your second array or you already subdivided into sub array
$groups = [];
foreach($array as $subArray) {
foreach ($subArray as $group) {
if (isset($groups[$group])) {
$groups[$group] += 1;
} else {
$groups[$group] = 1;
}
}
}

Grouping arrays by specified key value pair

Heys guys I have the following array:
Array
(
[0] => Array
(
[date] => 2017-03-31
[1] => 7.9950
)
[1] => Array
(
[date] => 2017-06-30
[1] => 8.3425
)
[2] => Array
(
[date] => 2017-03-31
[2] => 6.8250
)
[3] => Array
(
[date] => 2017-06-30
[2] => 10.7725
)
[4] => Array
(
[date] => 2017-03-31
[3] => 6.4950
)
[5] => Array
(
[date] => 2017-06-30
[3] => 7.3425
)
)
I need to merge all the values that share the same date into one array, preserving the original keys if possible. They're id's that I'll then json_encode to build a chart.
Array
(
[0] => Array
(
[date] => 2017-03-31
[1] => 7.9950
[2] => 6.8250
[3] => 6.4950
)
[1] => Array
(
[date] => 2017-06-30
[1] => 8.3425
[2] => 10.7725
[3] => 7.3425
)
)
Is there a way of achieving this? I've looked for similar questions, searched the php manual for functions like array_merge and alike, but couldn't achieve the desired result. Any help would be appreciated.
How about something like this:
$output = array();
foreach($input as $record){
foreach($record as $key => $value){
$output[$record['date']][$key] = $value;
}
}
//make it a regular array
$output = array_values($output);
Any duplicate data will appear only once.

Sorting and merge associative array (PHP)

I have 2 associative arrays:
[0] => Array (
[time] => 09:00:00
[name] => To do something 1
)
[1] => Array (
[time] => 09:30:00
[name] => To do something 2
)
[2] => Array (
[time] => 10:00:00
[name] => To do something 3
)
and
[0] => Array (
[time] => 09:00:00
)
[1] => Array (
[time] => 09:15:00
)
[2] => Array (
[time] => 09:30:00
)
[3] => Array (
[time] => 09:45:00
)
[4] => Array (
[time] => 10:00:00
)
Need to do another associative array:
if the field [time] in second array equals field [time] in first array then add [name];
if field [time] in second array not equals field [time] in first array then add empty [name].
Should look like this:
[0] => Array (
[time] => 09:00:00
[name] => To do something 1
)
[1] => Array (
[time] => 09:15:00
[name] =>
)
[2] => Array (
[time] => 09:30:00
[name] => To do something 2
)
[3] => Array (
[time] => 09:45:00
[name] =>
)
[4] => Array (
[time] => 10:00:00
[name] => To do something 3
)
After some puzzling I made it work for you.
This is the code you can use:
$array3 = array();
foreach ($array2 as $key2 => $value2){
$found = false;
foreach ($array1 as $key1 => $value1){
if ($array1[$key2]['time'] == $array2[$key2]['time']) {
$array3[$key2]['time'] = $array1[$key1]['time'];
$array3[$key2]['name'] = $array1[$key1]['name'];
$found = true;
}
}
if (!$found){
//add
$array3[$key2]['time'] = $array2[$key2]['time'];
$array3[$key2]['name'] = "";
}
}
print_r($array3);

Merging PHP Array

I have two arrays:
array1:
Array ( [id] => 1 [time] => 12:10:23 [date] => 2013-03-24 )
array2:
Array ( [id] => 2 [time] => 12:10:25 [date] => 2013-03-25 )
I would like to merge them so they result in the following (EDITED):
Array ( [id] => 1
=> 2
[time] => 12:10:23
=> 12:10:25
[date] => 2013-03-24
=> 2013-03-25
)
Is this possible?
If you actually mean to have the output:
Array (
Array ([id] => 1 [time] => 12:10:23 [date] => 2013-03-24 ),
Array ([id] => 2 [time] => 12:10:25 [date] => 2013-03-25 )
)
Then you want to use the following code:
$newArray = array($array1, $array2);
To get the element with the most recent date:
$most_recent = $newArray[0];
foreach($newArray as $compare)
{
if($compare['time'] > $most_recent['time'])
{
$most_recent = $compare;
}
}

Filter an PHP Array and consider the date range

I have the following array:
Array
(
[0] => Array
(
[date] => 2012-01-10
)
[1] => Array
(
[date] => 2012-01-11
)
[2] => Array
(
[date] => 2012-01-12
)
[3] => Array
(
[date] => 2012-01-15
)
[4] => Array
(
[date] => 2012-01-18
)
[5] => Array
(
[date] => 2012-01-19
)
)
And I want to combine the dates by his ranges. At the end I need something like the following
Array
(
[0] => Array
(
[from] => 2012-01-10
[till] => 2012-01-12
)
[1] => Array
(
[from] => 2012-01-15
[till] => 2012-01-15
)
[2] => Array
(
[from] => 2012-01-18
[till] => 2012-01-19
)
)
But it should also support a wide range. Something like 2012-01-22 till 2012-05-14. Anyone an idea or an hint?
your output does not contain the same dates as your input in the example above. how are the from/till dates decided? do we simply go through the array and alternate between from to?
function from_till($dates = array()){
$sorted = array();
for($i=0; $i<count($dates)-2; $i+=2){
$sorted[] = array('from' => $dates[$i], 'till' => $dates[$i+1];
}
return $sorted;
}
input:
$input = Array
(
[0] => Array
(
[date] => 2012-01-10
)
[1] => Array
(
[date] => 2012-01-11
)
[2] => Array
(
[date] => 2012-01-12
)
[3] => Array
(
[date] => 2012-01-15
)
[4] => Array
(
[date] => 2012-01-18
)
[5] => Array
(
[date] => 2012-01-19
)
);
output:
$output = from_till($input);
print_r($output);
output of print_r
Array
(
[0] => Array
(
[from] => 2012-01-10
[till] => 2012-01-11
)
[1] => Array
(
[from] => 2012-01-12
[till] => 2012-01-15
)
[2] => Array
(
[from] => 2012-01-18
[till] => 2012-01-19
)
);

Categories