In order to have an array output like
array(2) { ["2012-08-16"]=> array(1) { [0]=> array(2) { ["title"]=> string(24) "RLI Class #42 Graduation" ["event_date"]=> string(10) "2012-08-16" } } ["2012-08-24"]=> array(1) { [0]=> array(2) { ["title"]=> string(10) "Test Event" ["event_date"]=> string(10) "2012-08-24" } } }
I am trying the following
$array = array('title' => $mytitle, 'event_date' => $myyear."-".$mymonth."-".$myday);
$events[$array['event_date']] = $array['title'];
However the output I get is
array(1) { [""]=> NULL } array(1) { ["2012-8-24"]=> string(10) "Test Event" } array(1) { ["2012-8-16"]=> string(25) "RLI Class #42 Graduation" }
How should I change the code to let $events output in the desired array format?
EDIT Just in case a little background of the code will be helpful to find the solution, here it is
while ( $query->have_posts() ) : $query->the_post();
$mytime = get_post_meta($post->ID, 'wpcf-start-date', true); // this has 2 entries: 1>1347408000 and 2>1347408000
$mydate = date("Y-m-D",$mytime);
$mydatearr = getdate($mytime);
$myday = $mydatearr['mday']; // returns values 16 and 24
$mymonth = $mydatearr['mon']; // returns values 8 and 8
$myyear = $mydatearr['year']; // returns values 2012 and 2012
$mytitle = $post->post_title;
if (($myyear == $year) && ($mymonth == $month)) //The $year is declared earlier as 2012 and $month as 8
Next was my code which I replaced with
$events = array(); ## Declare the new array.
foreach ($array as $line) {
$event_date = $line['event_date']; ## Set event_date to a variable
$events[$event_date] = array( 'title' => $line['title'], 'event_date' => $event_date ); ###add new value to new array with key value }
var_dump($events);
And the endwhile
endwhile;
EDIT OK. I seem to have mistakenly deleted my $array declaration and earlier declared the $events array inside the loop. So I am updating the new code
$events = array(); ## Declare the new array.
while ( $query->have_posts() ) : $query->the_post();
$mytime = get_post_meta($post->ID, 'wpcf-start-date', true);
$mydate = date("Y-m-D",$mytime);
$mydatearr = getdate($mytime);
$myday = $mydatearr['mday'];
$mymonth = $mydatearr['mon'];
$myyear = $mydatearr['year'];
$mytitle = $post->post_title;
if (($myyear == $year) && ($mymonth == $month))
$array = array('title' => $mytitle, 'event_date' => $myyear."-".$mymonth."-".$myday);
foreach ($array as $line) {
$event_date = $line['event_date']; ## Set event_date to a variable
$events[$event_date] = array( 'title' => $line['title'], 'event_date' => $event_date ); ###add new value to new array with key value
}
var_dump($events);
endwhile;
The var_dump for $array gives
NULL
Warning: Invalid argument supplied for foreach() in /home/newmedia/public_html/dev/wp-gallerific/wp-content/themes/lajcc-theme/calendar.php on line 165
array(2) { ["title"]=> string(10) "Test Event" ["event_date"]=> string(9) "2012-8-24" } array(2) { ["title"]=> string(25) "RLI Class #42 Graduation " ["event_date"]=> string(9) "2012-8-16" }
And var_dump for $events outputs
Warning: Invalid argument supplied for foreach() in /home/newmedia/public_html/dev/wp-gallerific/wp-content/themes/lajcc-theme/calendar.php on line 165
array(0) { } array(2) { ["T"]=> array(2) { ["title"]=> string(1) "T" ["event_date"]=> string(1) "T" } [2]=> array(2) { ["title"]=> string(1) "2" ["event_date"]=> string(1) "2" } } array(3) { ["T"]=> array(2) { ["title"]=> string(1) "T" ["event_date"]=> string(1) "T" } [2]=> array(2) { ["title"]=> string(1) "2" ["event_date"]=> string(1) "2" } ["R"]=> array(2) { ["title"]=> string(1) "R" ["event_date"]=> string(1) "R" } }
I think you need to foreach()
through your array to get the desired output.
Give this a try:
$events = array(); ## Declare the new array.
foreach ($array as $line) {
$event_date = $line['event_date']; ## Set event_date to a variable
$events[$event_date] = array( 'title' => $line['title'], 'event_date' => $event_date ); ###add new value to new array with key value
}
var_dump($events); ##display the reults
Good luck!
Why are you executing
$events = array(); ## Declare the new array.
inside your while
? Each iteration basically erase all data set from previous iterations. Just put that line above your while
(outside, one live above) and var_dump($events)
after your endwhile
.