我有很多这样的记录:
2011-01-01, a, 1
2011-01-01, c, 5
2011-01-01, d, 3
2011-01-02, a, ...
第一个值是日期,第二个值是字符串(可以是a
或b
或c
或d
),第三个是数字。
如何在PHP中将所有记录添加到数组中,并且数组的结构如下:
array('2011-01-01' => array('a' => '1','b' => '0','c' => '5','d' => '3'), '2011-01-02' => ... ,)
这样日期变量是一个索引,而键是一个四元素数组,每个元素都是相应的记录号(第三个值)?
答案 0 :(得分: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;
}
答案 1 :(得分: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}}
答案 2 :(得分: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);
答案 3 :(得分: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);
答案 4 :(得分: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
}
请注意,部件会从其他帖子中复制一段时间。