1

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" } }  
2
  • 1
    Why would you want the key (event_date) also input as a value within your array? Wouldn't it be easier just to have an associative array with the event_date as key and the title as its value? Or is that not applicable for your application? Commented Aug 11, 2012 at 23:21
  • I guess it's not applicable for my application. I need the output exactly as desired. Have been trying a lot of things but just can't nail it. Commented Aug 11, 2012 at 23:32

2 Answers 2

0

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!

Sign up to request clarification or add additional context in comments.

3 Comments

In order to get the desired result, I'd change the 4th line of your code: $events[$event_date] = array( 'title' => $line['title'], 'event_date' => $event_date );
I apologize, I missed the data key/value completely! Thanks!
Nopes. It displays Warning: Invalid argument supplied for foreach() in /home/newmedia/public_html/dev/wp-gallerific/wp-content/themes/lajcc-theme/calendar.php on line 164 NULL Warning: Invalid argument supplied for foreach() in /home/newmedia/public_html/dev/wp-gallerific/wp-content/themes/lajcc-theme/calendar.php on line 164 array(0) { } Warning: Invalid argument supplied for foreach() in /home/newmedia/public_html/dev/wp-gallerific/wp-content/themes/lajcc-theme/calendar.php on line 164 array(0) { }
0

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.

3 Comments

Tried it outside the loop and still returns array(0) { } Warning: Invalid argument supplied for foreach() in /home/newmedia/public_html/dev/wp-gallerific/wp-content/themes/lajcc-theme/calendar.php on line 164 array(0) { } Warning: Invalid argument supplied for foreach() in /home/newmedia/public_html/dev/wp-gallerific/wp-content/themes/lajcc-theme/calendar.php on line 164 array(0) { }
Then you are not showing all the relevant code, because there is no way, with what you provided, that you'd get this warning.
You are right. I had mistakenly omitted the $array line from the last code. However, it still gives some errors. I have updated the code with the outputs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.