目前尚不清楚的結構,你會最終需要的東西。
我想你可能需要的東西是這樣的:
<?php
// initial an empty array
$maxCapArray=array();
// first run on a day
$timeStampA=mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$maxCapArray[$timestampA] = array('Book'=>'BLABLA','Author'=>'BLEBLE');
// second run on a different day
$timeStampB=mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$maxCapArray[$timestampB] = array('Book'=>'BLABLA','Author'=>'BLEBLE');
// third run on yet another day
$timeStampC=mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$maxCapArray[$timestampC] = array('Book'=>'BLABLA','Author'=>'BLEBLE');
// ...
// to retrieve the Author of a given timestamp
echo $maxCapArray[$timestampA]['Author'];
echo $maxCapArray[$timestampB]['Author'];
echo $maxCapArray[$timestampC]['Author'];
?>
第一行會刪除您之前獲得的所有數據。請以合理的方式使用它們。
另一種猜測:
<?php
// initial an empty array
$maxCapArray=array();
$timeStamp=mktime(0, 0, 0, date("m"), date("d"), date("Y"));
// suppose you want $maxCapArray[$timestamp]['Book'] and $maxCapArray[$timestamp]['author']
// to be arrays
if (!isset($maxCapArray[$timestamp])) $maxCapArray[$timestamp] = array();
$maxCapArray[$timestamp]['Book'][] = 'BLABLA';
$maxCapArray[$timestamp]['Author'][] = 'BLEBLE';
$maxCapArray[$timestamp]['Book'][] = 'BLABLA 2';
$maxCapArray[$timestamp]['Author'][] = 'BLEBLE 2';
// ...
// you should get an array:
var_dump($maxCapArray[$timestamp]['Author']);
?>
更新:看來你在尋找這個
<?php
// initial an empty array
$maxCapArray=array();
$timeStamp=mktime(0, 0, 0, date("m"), date("d"), date("Y"));
// suppose you want $maxCapArray[$timestamp]['Book'] and $maxCapArray[$timestamp]['author']
// to be arrays
if (!isset($maxCapArray[$timestamp])) $maxCapArray[$timestamp] = array();
$maxCapArray[$timestamp]['Author']['Book 1'] = 'Author 1';
$maxCapArray[$timestamp]['Author']['Book 2'] = 'Author 2';
$maxCapArray[$timestamp]['Author']['Book 3'] = 'Author 2';
// ...
// review your array:
var_dump($maxCapArray[$timestamp]['Author']);
// retrieve
$author_name = 'Author A';
$book_name = 'Book 1';
echo $maxCapArray[$timestamp][$author_name][$book_name];
?>
一些更多的建議
數組只爲做一個索引結構1具體情況。幾天後,你可能需要另一個。也許你需要按書搜索作者,或者按書搜索時間戳。誰知道?
我建議你使用關係型數據庫對這些情況。您可以在MySQL中創建數據庫表,如下所示:
+-----------+---------------------+------------------------------------------+---------------------+
| record_id | author | book | timestamp |
+-----------+---------------------+------------------------------------------+---------------------+
| 1 | Jules Verne | Twenty Thousand Leagues Under the Sea | 2013-01-12 12:24:00 |
| 2 | Jules Verne | Journey to the Center of the Earth | 2013-01-12 13:01:00 |
| 3 | William Shakespeare | Hamlet | 2013-01-12 16:51:00 |
+-----------+---------------------+------------------------------------------+---------------------+
MySQL將自動執行所有索引工作。
然後,當你需要的數據,就可以連接到數據庫,並宣讀:
<?php
// connect to
mysql_connect('localhost', 'db_user', 'db_password');
mysql_select_db('library_database');
// ask database of books of a certain timestamp and of certain author
$results = mysql_query('SELECT author FROM book_records WHERE
timestamp="2013-01-12 13:01:00" AND book="Journey to the Center of the Earth"');
// read the database's answer to $books array
$authors = array();
while (($row = mysql_fetch_assoc($results)) != FALSE) {
$authors = $row['author'];
}
// you may examine the array here
// this is a list of author of the specific timestamp
// and of specific book
var_dump($authors);
?>
如果您有其他需要,你可以更改代碼:
<?php
// connect to
mysql_connect('localhost', 'db_user', 'db_password');
mysql_select_db('library_database');
// if you want to have all the timestamp of a book and an author
// you may just do this
$results = mysql_query('SELECT timestamp FROM book_records WHERE
author="Jules Verne" AND book="Journey to the Center of the Earth"');
// read the database's answer to $books array
$timestamps = array();
while (($row = mysql_fetch_assoc($results)) != FALSE) {
$timestamps = $row['timestamp'];
}
// this is the list of related timestamp
var_dump($timestamps);
?>
不太明白。你有什麼輸入數據?什麼是你想要的輸出數據? – 2013-02-25 10:18:48
爲什麼'$ maxCapArray'在同一行上定義兩次?第二個定義將覆蓋第一個,所以難怪你只能得到最後一個項目。 – SDC 2013-02-25 14:29:26