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);
Related
After generating group array from an associative array, now the array is in the following format. And to make this group array I have used the following code:
foreach($upcoming_data_arr as $key => $item)
{
$arr[$item['year']][$key] = $item;
}
Array
(
[2018] => Array
(
[0] => Array
(
[id] => 6
[year] => 2018
[month] => 11
)
[1] => Array
(
[id] => 5
[year] => 2018
[month] => 12
)
[2] => Array
(
[id] => 4
[year] => 2018
[month] => 11
)
[3] => Array
(
[id] => 3
[year] => 2018
[month] => 11
)
)
)
Now I need another group array according to month within year group. And in this case group data array will be like following:
Array
(
[2018] => Array
(
[11] => Array
(
[0] => Array
(
[id] => 6
[year] => 2018
[month] => 11
)
[1] => Array
(
[id] => 4
[year] => 2018
[month] => 11
)
[2] => Array
(
[id] => 3
[year] => 2018
[month] => 11
)
)
[12] => Array
(
[0] => Array
(
[id] => 5
[year] => 2018
[month] => 12
)
)
)
)
How we will get this output?
Add one more "dimension" when you are creating an array identified by $item['month'] and let php decide last "dimension" key by []:
foreach($upcoming_data_arr as $key => $item) {
$arr[$item['year']][$item['month']][] = $item;
}
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);
This question already has answers here:
Get most recent date from an array of dates in "Y-m-d H:i:s" format
(12 answers)
Closed 9 years ago.
As an output from MySQL I got an array like this:
Array
(
[0] => Array
(
[name] => Date
[data] => Array
(
[0] => 2009-07-01
[1] => 2009-08-01
[2] => 2009-09-01
[3] => 2010-10-01
[4] => 2010-11-01
[5] => 2010-12-01
[6] => 2011-01-01
[7] => 2011-02-01
[8] => 2012-03-01
[9] => 2012-04-01
[10] => 2012-05-01
)
)
[1] => Array
(
[name] => Counts
[data] => Array
(
[0] => 13
[1] => 13
[2] => 15
[3] => 16
[4] => 17
[5] => 18
[6] => 19
[7] => 11
[8] => 14
[9] => 14
[10] => 15
)
)
)
For further developing I need to find the highest dates in each year. How to find last date of each year and get it's value from 'counts' array. For above code I would like to achieve something like this:
Array
(
[0] => Array
(
[name] => Date
[data] => Array
(
[1] => 2009-09-01
[2] => 2010-12-01
[3] => 2011-02-01
[4] => 2012-05-01
)
)
[1] => Array
(
[name] => Counts
[data] => Array
(
[1] => 15
[2] => 18
[3] => 11
[4] => 15
)
)
php has the max function . Read about it here it is very easy to use for your needs;
$arr = array('... your array ...');
$date = array();
$count = array();
foreach ($arr[0]['data'] as $key => $value) {
$year = substr($value, 0, 4);
if (!$date[$year] || $date[$year] < $value) {
$date[$year] = $value;
$count[$year] = $arr[1]['data'][$key];
}
}
$date = array_values($date); //reset the keys
$count = array_values($count); //reset the keys
$res = array(
array(
'name'=>'Date',
'data'=>$date,
),
array(
'name'=>'Count',
'data'=>$count,
)
);
print_r($res);
I am working on a statistics application and I want to output the amount of interactions that happened by day.
I have an multidimensional array that pulls all the information from the database, here is an example:
[0] => Array
(
[date] => 2012-07-26
[location] => 709c6d241674ca22
[action] => start_scan
)
[1] => Array
(
[date] => 2012-07-26
[location] => 709c6d241674ca22
[action] => scan_displayed
)
[2] => Array
(
[date] => 2012-07-27
[location] => 709c6d241674ca22
[action] => lower_device
)
[3] => Array
(
[date] => 2012-07-27
[location] => 709c6d241674ca22
[action] => how_to_use_displayed
)
[4] => Array
(
[date] => 2012-07-27
[location] => 709c6d241674ca22
[action] => raise_device
)
[5] => Array
(
[date] => 2012-07-28
[location] => 709c6d241674ca22
[action] => scan_displayed
)
I can work out what day each interaction occurred on by formatting the date:
date('D', strtotime('2012-07-26'));
My question is how do I count how many interactions happened on each day of the week and then output it, something like:
[Sunday] => 2
[Monday] => 3
[Tueday] => 1
[Wednesday] => 5
[Thursday] => 10
[Friday] => 4
[Saturday] => 9
Any suggestions are really appreciated!
$dates = array();
foreach($data as $item) {
$day = date('l', $item['date']);
$dates[$day]++;
}
var_dump($dates);
You may want to declare the array as $dates = array('Monday'=>0, 'Tuesday'=>0 .... ) to get the array containing the days with no interactions.
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]));
}