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]));
}
Related
really need your help with this array :
Array
(
[status] => 200
[error] =>
[resource] => Array
(
[type] => stats
[data] => Array
(
[0] => Array
(
[date] => Array
(
[year] => 2015
[month] => 12
)
[currency] => USD
[stats] => Array
(
[count] => 2
[total] => 2.53
[average] => 1.265
)
)
[1] => Array
(
[date] => Array
(
[year] => 2016
[month] => 1
)
[currency] => USD
[stats] => Array
(
[count] => 2
[total] => 15
[average] => 7.5
)
)
[2] => Array
(
[date] => Array
(
[year] => 2016
[month] => 1
)
[currency] => AUD
[stats] => Array
(
[count] => 1
[total] => 15
[average] => 15
)
)
[3] => Array
(
[date] => Array
(
[year] => 2016
[month] => 2
)
[currency] => AUD
[stats] => Array
(
[count] => 7
[total] => 1419.02
[average] => 202.71714285714
)
)
[4] => Array
(
[date] => Array
(
[year] => 2016
[month] => 2
)
[currency] => USD
[stats] => Array
(
[count] => 8
[total] => 2186.4
[average] => 273.3
)
)
[5] => Array
(
[date] => Array
(
[year] => 2016
[month] => 3
)
[currency] => USD
[stats] => Array
(
[count] => 3
[total] => 865
[average] => 288.33333333333
)
)
[6] => Array
(
[date] => Array
(
[year] => 2016
[month] => 3
)
[currency] => AUD
[stats] => Array
(
[count] => 19
[total] => 127279
[average] => 6698.8947368421
)
)
)
)
)
How i can get $value of [month] in the loop??
Help me please!
Do you mean you're trying to access the "month" index within date?
If so, you could try something like this:
Let's say $array is your array.
foreach($array['resource']['data'] as $data) {
echo $data['date']['month']; // Prints every month.
echo "<br/>";
}
Hope this helps.
Try array_column function (available since PHP 5.5)
// $arr is the initial array
$months = array_column($arr, 'month');
http://php.net/manual/ru/function.array-column.php
I have a multi dimensional array of items which all have a date. The array follows the format: $relatedItems[$year][$month][$day][]. Is there a way of sorting this so that it is in date order. I've tried krsort() bu i think that will just sort the year key e.g. [2014]. Sorry if this is vague I'm new to php.
Array
(
[2014] => Array
(
[01] => Array
(
[13] => Array
(
[0] => Array
(
[type] => news-blog
[title] => Jungle News
[date] => 13th January 2014
[url] => /sport/jungle-ultra/news/jungle-news/
[description] => Description goes here
)
)
[23] => Array
(
[0] => Array
(
[type] => gallery
[title] => New Gallery
[url] => /sport/jungle-ultra/galleries/new-gallery/
[images] => Array
(
[0] => Array
(
[title] => 10.jpg
[src] => /files/cache/978f98c61f63757334fbeec79fabe482_f65.jpg
)
[1] => Array
(
[title] => 9.jpg
[src] => /files/cache/1526727d35b28930b49c69b065a229de_f64.jpg
)
[2] => Array
(
[title] => 8.jpg
[src] => /files/cache/d1b67fefed5e7b7644ad9ec1fe8559ae_f63.jpg
)
[3] => Array
(
[title] => 7.jpg
[src] => /files/cache/80160e395233a15d1b1df4df67ec565d_f62.jpg
)
[4] => Array
(
[title] => 6.jpg
[src] => /files/cache/48575e2d53c487f99633a1105e7a322b_f61.jpg
)
[5] => Array
(
[title] => 4.jpg
[src] => /files/cache/9fe01089fc9656562fb7f082499439fc_f60.jpg
)
)
)
)
[27] => Array
(
[0] => Array
(
[type] => video
[title] => Test
[url] => /sport/jungle-ultra/video/test/
[thumb] => /files/5313/8996/9327/speaker-header-small.jpg
)
)
[24] => Array
(
[0] => Array
(
[type] => video
[title] => Rich reaches half way point
[url] => /sport/jungle-ultra/video/rich-reaches-half-way-point/
[thumb] => /files/7013/9039/4602/video-cover-test.jpg
)
)
[21] => Array
(
[0] => Array
(
[type] => audio
[title] => Day 1 - Update from Alexander Island
[url] => /sport/jungle-ultra/audio/day-1-update-alexander-island/
[thumb] => /files/7013/9039/4602/video-cover-test.jpg
)
)
)
)
)
You are correct in using ksort but you need to use a recursive function. Try using this:
function sort_by_date($dates){
if(is_array($dates)){ // If it is an array
ksort($dates); // Sort the array keys
foreach($dates as $date){ // Loop through the keys
sort_by_date($date); // And call this recursive function on each of the child arrays
}
}
}
Usage:
$array = array(...); // Your date array
sort_by_date($array); // New function for sorting
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 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
)
);
I have the following code for the array below
$arrCompany = array();
foreach($users as $k => $user){
if(!in_array($user['User']['company_id'], $arrCompany)){
$arrCompany['company'][] = $user['User']['company_id'];
}else{}
}
what I am trying to do is just have one entery of company id so I can add users under it but for some reasons its not working
here is my array
Array
(
[0] => Array
(
[User] => Array
(
[id] => 1
[company_id] => 20
[type] =>
)
)
[1] => Array
(
[User] => Array
(
[id] => 6
[company_id] => 21
[type] =>
)
)
[2] => Array
(
[User] => Array
(
[id] => 7
[company_id] => 22
[type] =>
)
)
[3] => Array
(
[User] => Array
(
[id] => 14
[company_id] => 21
[type] =>
)
)
[4] => Array
(
[User] => Array
(
[id] => 15
[company_id] => 22
[type] =>
)
)
[5] => Array
(
[User] => Array
(
[id] => 16
[company_id] => 21
[type] =>
)
)
)
)
when I do var_dump...i get this
Array
(
[company] => Array
(
[0] => 20
[1] => 21
[2] => 22
[3] => 21
[4] => 22
[5] => 21
)
)
you can see the company has been repeated
$arrCompany = array('company'=>array());
foreach($users as $k => $user){
if(!in_array($user['User']['company_id'], $arrCompany['company'])){
$arrCompany['company'][] = $user['User']['company_id'];
}
}