2013-02-25 72 views
0

我想建立具有密钥作为时间戳阵列,以及由于某种原因,它的工作原理,当我手动建立它,但是当我尝试合并它不工作新物品。PHP阵列Key作为时间戳和合并工作不

我读合并的最快方法是:[]数组,但它增加了[0],[1]在关键的面前,我需要找到解决这个工作,也索引。因为我需要通过时间戳访问密钥。

$timeStamp=mktime(0, 0, 0, date("m"), date("d"), date("Y")); 

$maxCapArray=array(); 

// IDEAL MERGING BUT NOT WORKING: $maxCapArray[]= $timeStamp => array('Book'=>'BLABLA','Author'=>'BLOBLO'); 

// (string) NOT WORKING: $maxCapArray[]= (string)$timeStamp => array('Book'=>'BLABLA','Author'=>'BLUBLU'); 

$maxCapArray = array($timeStamp => array('Book'=>'BLABLA','Author'=>'BLEBLE')); $maxCapArray = array($timeStamp => array('Book'=>'BLABLA','Author'=>'BLIBLI')); 

// MANUALLY ONLY TAKES THE LAST ITEM: 

echo $maxCapArray[$timeStamp]['Author']; 

任何帮助将不胜感激。

+2

不太明白。你有什么输入数据?什么是你想要的输出数据? – 2013-02-25 10:18:48

+0

为什么'$ maxCapArray'在同一行上定义两次?第二个定义将覆盖第一个,所以难怪你只能得到最后一个项目。 – SDC 2013-02-25 14:29:26

回答

1

不能添加元素,如$maxCapArray[]= $timeStamp => array('Book'=>'BLABLA','Author'=>'BLOBLO');

  • 你可以这样做:

    $ V =阵列();
    $ V [] =阵列( “A”=>数组(1,2,3));

这将得到:

print_r($v); 
Array ([0] => Array ([a] => Array ([0] => 1 [1] => 2 [2] => 3))) 
  • 或该:

    $ V [ “一个”] =阵列(1,2,3);

将得到:

print_r($v); 
Array ([a] => Array ([0] => 1 [1] => 2 [2] => 3)) 
+0

嗨Fedorqui, 我做到了这一点:$ v [$ timeStamp] = array(1,2,3);并尝试像这样手动回显它:echo $ v [$ timestamp] [0],并尝试了$ timestamp,'$ timestamp',字符串($ timestamp),strval($ timestamp) 但不起作用,任何想法? – Sebastian 2013-02-25 14:11:40

+0

这应该工作:'$ timestamp =“aaaa”; $ v [$ timestamp] = array(1,2,3); print_r($ v); 回声“嗨,这是”。 $ V [$时间戳] [0];'。问题是你必须保持索引'$ timestamp'。如果$ timestamp再次被定义,您将无法访问它。因此,使用'print_r($ v)'来检查数组是如何定义的。 – fedorqui 2013-02-25 14:16:35

1

目前尚不清楚的结构,你会最终需要的东西。

我想你可能需要的东西是这样的:

<?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); 

?> 
+0

嗨,考拉,版本2更接近我想要的。 现在更清楚如何创建数组。 但要检索它,我有$ timestamp&$ Book,并且我想获得 作者。我该如何调用数组? //这给了作者,但我需要知道正确的索引号,我没有 回声$ maxCapArray [$时间戳] [“作者”] [0]“。
'; echo $ maxCapArray [$ timestamp] ['Author'] [1]。'
'; 它应该是这样的: echo $ maxCapArray [$ timestamp] ['Author'] [$ Book]。'
'; 并且应该给 - > Author for specific $ timestamp&Book – Sebastian 2013-02-25 13:07:49

+0

更新了我的答案。请阅读**更新**部分。我的建议是使用SQLite或MySQL数据库,你。请看看我的例子。如果你觉得它有用,去搜索互联网了解更多信息。 – 2013-02-25 14:13:44

0

你可能正在寻找此:

$timeStamp = mktime(0, 0, 0, date("m"), date("d"), date("Y")); 
$maxCapArray = array(); 

$maxCapArray[$timeStamp] = array(); 
$maxCapArray[$timeStamp][] = array('Book'=>'BLABLA','Author'=>'BLEBLE'); 
$maxCapArray[$timeStamp][] = array('Book'=>'BLABLA','Author'=>'BLIBLI'); 

echo $maxCapArray[$timeStamp][0]['Author']; // == BLEBLE 

被警告,如果您使用array_merge()合并数字lly索引数组,键将从零开始重新索引,请参阅http://php.net/manual/en/function.array-merge.php