2012-01-10 18 views
0

我喜歡這些多個記錄:PHP陣列元件,和變量作爲索引

2011-01-01, a, 1 
2011-01-01, c, 5 
2011-01-01, d, 3 
2011-01-02, a, ... 

的第一個值的日期,第二個是一個字符串(可以是abcd ),第三個是一個數字。

如何添加的所有記錄到PHP中的數組,數組的結構是這樣的:

array('2011-01-01' => array('a' => '1','b' => '0','c' => '5','d' => '3'), '2011-01-02' => ... ,) 

以便日期變量是一個指標,關鍵是四元素的數組,每個元素是相應的記錄號碼(第三個值)?

+0

初始記錄是如何實際存儲的?作爲一個數組結構,在一個文件或數據庫中? – cmbuckley 2012-01-10 00:13:18

+0

從數據庫讀取,存儲爲數組 – DrXCheng 2012-01-10 00:16:59

回答

1

這個怎麼樣(假設你的輸入數據的格式) :

$input = array(
    array('2011-01-01', 'a', '1'), 
    array('2011-01-01', 'c', '5'), 
    array('2011-01-01', 'd', '3'), 
    array('2012-01-01', 'a', '1') 
); 

// used to populate the default strings (a b c d) 
$strings = range('a', 'd'); 
$stringDefaults = array_combine($strings, array_fill(0, count($strings), '0')); 

$output = array(); 

foreach ($input as $row) { 
    list ($date, $string, $number) = $row; 

    if (!isset($output[$date])) { 
     $output[$date] = $stringDefaults; 
    } 

    $output[$date][$string] = $number; 
} 

echo json_encode($output); 
+0

不會那個json_encode輸出括號不花括號的數組嗎? – AlienWebguy 2012-01-10 00:27:32

+0

見http://codepad.org/7JIYcFhi;這些數組是相關聯的,因此將使用大括號。在任何情況下,問題中的JSON表示似乎只是偶爾描述結構,而不是預期輸出,但無論如何我都將其包含在內。 – cmbuckley 2012-01-10 00:30:40

1

假設這是一個很大的字符串,請將它拆分爲行,然後再分隔逗號。對於每一行,測試日期是否已經存在的一個關鍵,如果確實如此,添加到它,否則,創建它:

$string = "2011-01-01, a, 1 
2011-01-01, c, 5 
2011-01-01, d, 3 
2011-01-02, a, 1"; 

// Array to hold it all... 
$array = array(); 
$lines = explode("\n", $string); 
foreach ($lines as $line) { 
    // Explode the line on the comma into 3 variables 
    list($date, $key, $num) = explode(",", $line); 
    $date = trim($date); 
    $key = trim($key); 
    $num = intval($num); 

    // Create an array key for the current $date if it doesn't exist 
    // as a new array 
    if (!isset($array[$date])) $array[$date] = array(); 

    // And assign the number with the corresponding letter key to the array 
    // for this date. 
    $array[$date][$key] = $num; 
} 
+0

+1 - 但不要忘記'修剪'。 – 2012-01-10 00:17:35

+0

@JosephSilber謝謝,並編輯。 – 2012-01-10 00:19:52

+0

我認爲這會錯過輸出「b」:「0」作爲輸出的一部分(即所有可能的字符串a,b,c,d應該以默認數字0顯示)? – cmbuckley 2012-01-10 00:26:13

1
$records = array(
    0 => array('2011-01-01', 'a', '1'), 
    1 => array('2011-01-01', 'c', '5'), 
    2 => array('2011-01-01', 'd', '3'), 
    3 => array('2012-01-01', 'a', '1') 
); 

$tmp_arr = array(); 
$tmp_dates = array(); 
$tmp_str = ''; 

foreach ($records as $record) { 
    $tmp_arr[$record[0]][$record[1]] = $record[2]; 
} 

foreach ($tmp_arr as $date => $record) { 
    $tmp_params = array(); 
    foreach ($record as $key => $val) { 
     $tmp_params[] = $key . ':' . $val; 
    } 
    $tmp_dates[] = $date . ':' . '{' . implode(',', $tmp_params) . '}'; 
} 

$tmp_str = '{' . implode(',', $tmp_dates) . '}'; 

echo $tmp_str; // {2011-01-01:{a:1,c:5,d:3},2012-01-01:{a:1}} 
+0

與[邁克爾的回答](http://stackoverflow.com/a/8796986/283078),OP似乎建議每個日期數組本身應該是一個4元素的數組本身,默認值爲0,如果該行是不存在於輸入中。 – cmbuckley 2012-01-10 00:27:55

0

前提是你的數據在一個文本文件(.txt):

$records = file_get_contents("filename.txt"); 

// $records will be equal to the following: (on windows, it would be \r\n instead of just \n) 
// $records = "2011-01-01, a, 1\n2011-01-01, c, 5\n2011-01-01, d, 3\n 2011-01-02, d, 3"; 

$records = explode("\n", $records); 

$output = array(); 

for ($i = 0; $i < count($records); $i++) { 

    $rec = explode(",", $records[$i]); 
    $key = $rec[0]; 

    if ($output[$key] == null) 
     $output[$key] = array(); 

    if (!array_key_exists("a", $output[$key])) 
     $output[$key]["a"] = 0; 

    if (!array_key_exists("b", $output[$key])) 
     $output[$key]["b"] = 0; 

    if (!array_key_exists("c", $output[$key])) 
     $output[$key]["c"] = 0; 

    if (!array_key_exists("d", $output[$key])) 
     $output[$key]["d"] = 0; 

    $elem = trim($rec[1]); 
    $value = trim($rec[2]); 
    $output[$key][$elem] = intval($value); 

} 

var_dump($output); 
+0

哦,我只是讀你的意見之一,你是從數據庫讀取和存儲在一個數組,否則你最初發布的格式看起來像一個文本流:)讓我知道如果你有麻煩翻譯上述代碼從一個閱讀數組而不是文本文件。 – 2012-01-10 00:50:14

0

好吧,這裏就是你要做的。

$records = array(
    0 => array('2011-01-01', 'a', '1'), 
    1 => array('2011-01-01', 'c', '5'), 
    2 => array('2011-01-01', 'd', '3'), 
    3 => array('2012-01-01', 'a', '1') 
); 

$dateArray = array(); 
$baseArray = array('a' => '0','b' => '0','c' => '0','d' = '0'); 

foreach($records as $record) 
{ 
    $dateArray[$record[0]][$record[1]] = $record[2]; //You could actually do this while pulling in the info from the DB 
} 

foreach($dateArray as $date => $array) 
{ 
    $missingArray = array_diff_key($array,$baseArray); //Determine what keys are missing using the base array, get keys and default values 
    $dateArray[$date] = array_merge($array,$missingArray); //Merge the missing parts with the current array 
} 

請注意,從其他帖子複製時間的零件。