Php 在多维数组中计算一列中的频率…并与另一列一起工作
我有一个像这样的csv文件,当然还有更多的行。它有4列Php 在多维数组中计算一列中的频率…并与另一列一起工作,php,regex,Php,Regex,我有一个像这样的csv文件,当然还有更多的行。它有4列 433444 20 2009 Part_description_433444 432222 15 2009 Part_description_432222 535333 10 2010 Part_description_535333 433444 15 2009 Part_description_433444 432222 .4 2012 Part_description_432222 535333 20 2010 Part_descrip
433444 20 2009 Part_description_433444
432222 15 2009 Part_description_432222
535333 10 2010 Part_description_535333
433444 15 2009 Part_description_433444
432222 .4 2012 Part_description_432222
535333 20 2010 Part_description_535333
我想计算第一列中零件号出现的次数。第二列表示该行中每个关联零件号的数量。所以我想计算第一列中零件号出现的次数…然后用第二列乘以每次出现的数量。此外,我想按每年的次数/数量进行排序,这是第3列
到目前为止,我只有多维数组格式
$formattedArr = array();
$filename = "parts_compile.csv";
if (($handle = fopen($filename, "r")) !== FALSE) {
$key = 0; // Set the array key.
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$count = count($data); //get the total keys in row
//insert data to our array
for ($i=0; $i < $count; $i++) {
$formattedArr[$key][$i] = $data[$i];
}
$key++;
}
fclose($handle); //close file handle
}
//csv to multidimensional array in php
echo $formattedArr[2][0];
$formattedArr=array();
$filename=“parts\u compile.csv”;
if($handle=fopen($filename,“r”)!==FALSE){
$key=0;//设置数组键。
while(($data=fgetcsv($handle,1000,“,”)!==FALSE){
$count=count($data);//获取行中的键总数
//将数据插入我们的数组
对于($i=0;$i<$count;$i++){
$formattedArr[$key][$i]=$data[$i];
}
$key++;
}
fclose($handle);//关闭文件句柄
}
//php中csv到多维数组的转换
echo$formattedArr[2][0];
我想我应该在第一列中找到每个元素的匹配数,在第二列中保留对数量的引用,但我不知道该怎么做。非常感谢您的帮助
谢谢,
另一种可能的解决方案:
<?php
$formattedArr = array();
$filename = "parts_compile.csv";
if (($handle = fopen($filename, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//insert data to our array
$formattedArr[] = $data;
}
fclose($handle); //close file handle
}
echo "<pre>";
print_r($formattedArr);
echo "</pre>";
$idx_id = 0;
$idx_amount = 1;
$idx_year = 2;
$idx_name = 3;
$res = array();
foreach($formattedArr as $a) {
$id = $a[$idx_id];
$amount = $a[$idx_amount];
$year = $a[$idx_year];
$name = $a[$idx_name];
if(!isset($res[$id])) {
$res[$id] = array('name'=>$name, 'quant_per_year'=>array());
}
if(!isset($res[$id]['quant_per_year'][$year])) {
$res[$id]['quant_per_year'][$year] = 0;
}
$res[$id]['quant_per_year'][$year] += $amount;
}
echo "<pre>";
print_r($res);
echo "</pre>";
我不知道你要怎么分类 此外,我想按每年的次数/数量进行排序,这是第3列 有点模糊
哦,这就是他的意思吗?当一个给定的零件可能在多行上有多个年份时,我对如何按年份排序感到困惑。现在我明白了…我希望这就是他的意思,我真的不知道。我会这样做,并通过javascript处理排序和总和。
<?php
$formattedArr = array();
$filename = "parts_compile.csv";
if (($handle = fopen($filename, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//insert data to our array
$formattedArr[] = $data;
}
fclose($handle); //close file handle
}
echo "<pre>";
print_r($formattedArr);
echo "</pre>";
$idx_id = 0;
$idx_amount = 1;
$idx_year = 2;
$idx_name = 3;
$res = array();
foreach($formattedArr as $a) {
$id = $a[$idx_id];
$amount = $a[$idx_amount];
$year = $a[$idx_year];
$name = $a[$idx_name];
if(!isset($res[$id])) {
$res[$id] = array('name'=>$name, 'quant_per_year'=>array());
}
if(!isset($res[$id]['quant_per_year'][$year])) {
$res[$id]['quant_per_year'][$year] = 0;
}
$res[$id]['quant_per_year'][$year] += $amount;
}
echo "<pre>";
print_r($res);
echo "</pre>";
Array
(
[433444] => Array
(
[name] => Part_description_433444
[quant_per_year] => Array
(
[2009] => 35
)
)
[432222] => Array
(
[name] => Part_description_432222
[quant_per_year] => Array
(
[2009] => 15
[2012] => 0.4
)
)
[535333] => Array
(
[name] => Part_description_535333
[quant_per_year] => Array
(
[2010] => 30
)
)
)
<?php
$filename = "parts_compile.csv";
$parts = array();
if (($handle = fopen($filename, "r")) !== FALSE) {
// the data is dilimited with 1 white space character in the question
while (($columns = fgetcsv($handle, 0, " ")) !== FALSE) {
$prod_number = $columns[0];
$quantity = $columns[1];
$year = $columns[2];
if (!isset($parts[$prod_number])) {
$parts[$prod_number] = array('name' => $columns[3], 'occurances' => 0, 'quantity' => 0, 'years' => array());
}
$parts[$prod_number]['occurances']++;
$parts[$prod_number]['quantity'] += $quantity;
if (!isset($parts[$prod_number]['years'][$year])) {
$parts[$prod_number]['years'][$year] = 0;
}
$parts[$prod_number]['years'][$year] += $quantity;
}
fclose($handle);
foreach ($parts as &$part) {
arsort($part['years']);
}
echo '<pre>';
print_r($parts);
}
<pre>
Array
(
[433444] => Array
(
[name] => Part_description_433444
[occurances] => 2
[quantity] => 35
[years] => Array
(
[2009] => 35
)
)
[432222] => Array
(
[name] => Part_description_432222
[occurances] => 2
[quantity] => 15.4
[years] => Array
(
[2009] => 15
[2012] => 0.4
)
)
[535333] => Array
(
[name] => Part_description_535333
[occurances] => 2
[quantity] => 30
[years] => Array
(
[2010] => 30
)
)
)