php - Show duplicate elements from an array -


i'm making calender , want show elements (public holiday etc.) array.

$holidays = array (     "20130101" => "new year's day",     "20130101" => "school holiday",     "20130126" => "australia day", ... );  $today = '20130101'; foreach ($holidays $key => $val) {     if ($today == $key) echo $val; else; } 

but shows "school holiday" last element in array want show both "new year's day" , "school holiday".

any advice on how approach problem? thank in advance.

you cannot have 2 elements same key in array. this:

$holidays = array (     "20130101" => "new year's day",     "20130101" => "school holiday",     "20130126" => "australia day" ); 

evaluates to

[     "20130101" => "school holiday",     "20130126" => "australia day" ] 

so it's no surprise 1 result in loop.

consider this:

$holidays = array (     array ("date" => "20130101", "name" => "new year's day"),     array ("date" => "20130101", "name" => "school holiday"),     array ("date" => "20130126", "name" => "australia day") );  $today = '20130101';  foreach ($holidays $holiday)  {     if ($holiday["date"] == $today)      {         echo $holiday["name"] . "\n";     } } 

Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -