目前尚不清楚您最终需要什么结构。
我想你可能需要这样的东西:
<?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);
?>