[PHP] 二維動態陣列
現在先假設是取了一堆date的陣列 格式為"YY-MM-DD"
$dateQuery = "select date from `healthrecord_testresult_sleep` where `uid`=".$uid." ORDER BY `rid` DESC";
$date = mysql_query($query) or die('Query failed : ' . mysql_error());
$allDate = array();
while($dateArray = mysql_fetch_assoc($date))
{
$allDate[] = $dateArray['date'];
}
$dateYear = array();
$dateMonth = array();
$dateDay = array();
// 怕會把重複的value加到年、月、日的array中,故以in_array()去過濾
foreach($allDate as $value)
{
list($year, $month, $day) = split('[/.-]', $value);
if(!in_array($year, $dateYear))
{
$dateYear["$year"] = $year;
}
if(!in_array($month, $dateMonth))
{
$dateMonth["$month"] = $month;
}
if(!in_array($day, $dateDay))
{
$dateDay["$day"] = $day;
}
}
print_r($dateYear);
echo '<br/>';
print_r($dateMonth);
echo '<br/>';
print_r($dateDay);
echo '<br/>';
echo 'Year '.$dateYear['2010'];
留言
張貼留言