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);
Related
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.
What I want to do
here is the main array
Array
(
[0] => Array
(
[Culture] => Array
(
[id] => 8
[title] => test123
[description] => test123
[year] => 2012
[photo] => test123.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[1] => Array
(
[Culture] => Array
(
[id] => 9
[title] => here title
[description] => here title
[year] => 2012
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[2] => Array
(
[Culture] => Array
(
[id] => 11
[title] => here title 2
[description] => here title 2
[year] => 2012
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[3] => Array
(
[Culture] => Array
(
[id] => 12
[title] => here title 3
[description] => here title 3
[year] => 2013
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[4] => Array
(
[Culture] => Array
(
[id] => 13
[title] => here title 4
[description] => here title 4
[year] => 2014
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[5] => Array
(
[Culture] => Array
(
[id] => 14
[title] => here title 5
[description] => here title 5
[year] => 2015
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
)
now from this array I want array of year (by key)
like:
Array
(
[0]=>2012
[1]=>2013
[2]=>2014
[3]=>2015
)
Loop through your array and assign those years to a new array with their keys intact.
$years=array();
foreach($yourArray as $key=>$value)
{
$years[$key]=$value["Culture"]["year"];
}
$years = array_unique($years);
print_r($years);
You can first loop through the array with a foreach loop and then use array_unique to get the array of years:
$years = array();
foreach ($records as $record) {
$years[] = $record['Culture']['year'];
}
$years = array_unique($years);
Demo!
$years = [];
foreach($arr as $newarray){
$years[] = $newarray['Culture']['year'];
}
$years = array_unique($years);
The new array years will now hold all the years in the old array. array_unique will get rid of all duplicate years.
My way is to use array-walk that takes anonymous function to fill the array, the solution will be in only one line of code.
Its working for me
foreach($cultures as $row)
{
$year[]=$row['Culture']['year'];
}
$year = array_unique($year);
$year = array_values($year);
echo "<pre>";print_r($year);
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.
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;
}
}
I Have an array which looks like this:
Array
(
[0] => Array
(
[0] => Array
(
[product] => 2003
[date] => 2010-09-15 13:27:35
[status] => 3
)
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 6
)
)
[1] => Array
(
[0] => Array
(
[product] => 2003
[date] => 2010-09-12 13:27:35
[status] => 1
)
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 4
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
)
[2] => Array
(
[0] =>
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
)
I want to "collapse" each second dimension array and obtain the max DATE value and the max status value.So the first index would return 2010-09-18 13:27:35 and '6' etc.
The problem is further complicated by the empty array in the last index. I would like to use this empty array and report it as the MAX date and status.
Thank in advance!
Thanks for looking everybody. I figured it out.
$date=array();
$status=array();
$availability=array();
foreach($set as $key => $value)
{
foreach($value as $value2)
{
if(isset($value2[1]))
{
$date[$key][]=$value2[1];
$status[$key][]=$value2[2];
}
else
{
$date[$key][]='2022-09-18 13:27:35';
$status[$key][]='0';
}
}
$availability[$key]=array(max($date[$key]),min($status[$key]));
}