2012-01-16 134 views
-1

我有對象的數組PHP數組對象轉換

([date]=>2012-01-09, [count]=>5), 
([date]=>2012-01-11, [count]=>7), 
([date]=>2012-01-12, [count]=>5) 

什麼是數組的數組對象或數組轉換

([date]=>2012-01-08, [count]=>0), 
([date]=>2012-01-09, [count]=>5), 
([date]=>2012-01-10, [count]=>0), 
([date]=>2012-01-11, [count]=>7), 
([date]=>2012-01-12, [count]=>5), 
([date]=>2012-01-13, [count]=>0), 
([date]=>2012-01-14, [count]=>0) 

假設給定的起始日期2012-01-08最好的辦法和結束日期2012-01-14

回答

1
  1. 生成從STARTDATE日期ENDDATE
  2. 創建一個空關聯數組來存儲您的日期數據。
  3. 遍歷#1中的一個所產生的所有的日期由一個
  4. 如果在初始陣列存在對於當前日期的計數,存儲的是,在#2創建的數組,否則存儲計數爲0時,與陣列鍵爲(string) $currentDate
  5. 步驟#2的數組現在將具有所需的數據。

轉換成PHP代碼:)

0

這個我不會爲你寫的代碼,但我會爲您提供的算法

$compArray = new array(); 

loop from startdate to enddate as date 
{ 
    if (in_array(date, yourarray)) 
     copy the entry from yourarray to compArray 
    else 
     enter date into compArray with a count of 0 
}