如何按子数组对数组进行分组并重新分配PHP中的键
问题描述:
我查看了所有类似的问题和示例(有很多),但仍无法使其工作(尽管我认为我很接近) 。如何按子数组对数组进行分组并重新分配PHP中的键
我有一个文本文件拉到员工安排的数组...
自定:
123,Joe,20120208,0845,1645 123,Joe,20120209,0800,1600 456,Sue,20120208,0900,1700 456,Sue,20120209,0700,1500 ...
我的代码来生成数组:
$schedule = file_get_contents('./schedule.txt'); $s = explode("\n", $schedule); for ($i = 0; $i < count($s); $i++) { $s[$i] = explode(",", $s[$i]); } print_r($s);
输出是:
Array ( [0] => Array ( [0] => 123 [1] => Joe [2] => 20120208 [3] => 0845 [4] => 1645 ) [1] => Array ( [0] => 123 [1] => Joe [2] => 20120209 [3] => 0800 [4] => 1600 ) [2] => Array ( [0] => 456 [1] => Sue [2] => 20120208 [3] => 0900 [4] => 1700 ) [3] => Array ( [0] => 456 [1] => Sue [2] => 20120209 [3] => 0700 [4] => 1500 ) )
我不是设法集团员工凭身份证在一个新的多维阵列的输出是这样的:
Array ( [123] => Array ( [0] => Array ( [0] => Joe [1] => 20120208 [2] => 0845 [3] => 1645 ) [1] => Array ( [0] => Joe [1] => 20120209 [2] => 0800 [3] => 1600 ) ) [456] => Array ( [0] => Array ( [0] => Sue [1] => 20120208 [2] => 0900 [3] => 1700 ) [1] => Array ( [0] => Sue [1] => 20120209 [2] => 0700 [3] => 1500 ) ) )
我不能为我的生命换我围绕着如何改变我现有的阵列到这个新头(不同的)输出... 我想这一点,但还是一无所获:
$newArr = array(); foreach($s as $k => $v) { if(!isset($newArr[$v[0]])) { $newArr[$v[0]] = array(); } $newArr[$v[0]][] = array($v[0]); }
虽然我的输出现在是:
Array ( [123] => Array ( [0] => Array ( [0] => 123 ) [1] => Array ( [0] => 123 ) ) [456] => Array ( [0] => Array ( [0] => 456 ) [1] => Array ( [0] => 456 ) ) )
有什么想法?我知道我失去了一些东西在我的周围行代码:
$newArr[$v[0]][] = array($v[0]);
我试图将其更改为:
$newArr[$v[0]][] = array(0 => $v[1], 1 => $v[2], 2 => $v[3], 3 => $v[4]);
这给我正确的输出,但我现在得到了一个未定义的偏移误差。另外,如果钥匙在子阵列更改的数量,这个代码现在仅限于4我明确地进入...
答
这应该是足够了,如果我理解你的权利:)
$lines = file('./schedule.txt');
$employees = array();
foreach ($lines as $line)
{
$chunks = explode(',', $line);
$employees[$chunks[0]][] = array_slice($chunks, 1);
}
print_r($employees);
答
<?php
//read the file into an array with each line being an entry
$schedule = file('./schedule.txt');
//empty array
$s = array();
//loop through file lines
foreach($schedule as $row){
//explode on comma
$row = explode(",", trim($row));
//if the id doesn't exist yet...
if(!isset($s[$row[0]])){
//...make it an empty array
$s[$row[0]] = array();
}
//add the row under the id
$s[$row[0]][] = $row;
}
print_r($s);
?>
答
您可以使用fscanf
:
$handle = fopen('./schedule.txt', 'r');
$employees = array();
while ($line = fscanf($handle, '%d,%s')) {
$employees[$line[0]][] = explode(',', $line[1]);
}
fclose($handle);
print_r($employees);
答
有一个叫fgetcsv
轻松地抓取逗号分隔值的功能。不需要自己做。
此代码还可以解决你的问题,妥善处理空行:
if (($handle = fopen("schedules.csv", "r")) !== FALSE) {
$schedules = array();
while (($data = fgetcsv($handle)) !== FALSE) {
if ($data[0] !== null) { //blank lines return an array containing null
$schedules[$data[0]][] = array_slice($data, 1);
}
}
fclose($handle);
//do what you need to
} else {
// file failed to open
}
注意,我指定的文件.csv
而不是.txt
。我建议您将以逗号分隔的值保存为.csv
文件。也许甚至可以定义文件中的每一列。
哇,看起来我实际上追求的都是错的 - 完美无缺。感谢您的快速。 – MonkishTypist 2012-02-16 23:41:32