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
Post a Comment