如何从php中的CSV转储中提取结构化数据

时间:2011-06-06 14:12:40

标签: php arrays csv fgetcsv

我正在尝试在PHP中加载CSV。

我有一个CSV文件,包含3列简单值。

我已经设法能够遍历每一行,并输出每个值,但我需要根据唯一值并根据行中的某个位置有条件地获取数据。

例如,对于a列中的每个唯一值,我需要根据列b的值生成一个数据对象数组,使用列c作为其中一个值的条件。

我的CSV:

1   123 0
1   124 0
1   125 0
1   126 0
1   127 0
1   128 0
1   129 0
1   130 1
1   131 1
1   132 1
1   133 1
1   134 1
1   135 1
2   123 0
2   124 0
2   125 0
2   126 1
2   127 1
2   128 1
2   129 1
2   130 1
2   131 1
2   132 1
2   133 1
2   134 1
2   135 1
3   256 0
3   456 0
3   321 0
3   489 0
3   965 0
3   652 1
3   741 1

我用来查询的代码:

<?php
$stack = array();
if (($han = fopen("sample.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($han, 50, ",")) !== FALSE) {
        array_push($stack, $data); //push each arr1 into arr2(stack)
    }
    fclose($han);
}
sort($stack); //sort arr2
$newarray = array();
foreach($stack as $val){
$lineid = $val[0];
$segmentid = $val[1];
$action = $val[2];
$newarray[$lineid][$segmentid] = $action;
}
print_r($newarray);
?>

输出

Array
(
    [1] => Array
        (
            [123] => 0
            [124] => 0
            [125] => 0
            [126] => 0
            [127] => 0
            [128] => 0
            [129] => 0
            [130] => 1
            [131] => 1
            [132] => 1
            [133] => 1
            [134] => 1
            [135] => 1
        )

    [2] => Array
        (
            [123] => 0
            [124] => 0
            [125] => 0
            [126] => 1
            [127] => 1
            [128] => 1
            [129] => 1
            [130] => 1
            [131] => 1
            [132] => 1
            [133] => 1
            [134] => 1
            [135] => 1
        )

    [3] => Array
        (
            [256] => 0
            [321] => 0
            [456] => 0
            [489] => 0
            [652] => 1
            [741] => 1
            [965] => 0
        )

)

psuedocode我想要实现的目标

for each unique column_a value {
grab all column_b's within this unique column_a and for each
    $column_b_object = new stdClass;
    $column_b_object->id = $column_b_value;
      if (column_c_value = "0") {
        $column_b_object->zero = "no";
      }
    $column_bs[] = $column_b_object;
    execute something, reset, move to next unique column_a
}

编辑解决方案:

<?php
$stack = array();
if (($han = fopen("sample.csv", "r")) !== FALSE) {
while (($data = fgetcsv($han, 50, ",")) !== FALSE) {
array_push($stack, $data); //push each arr1 into arr2(stack)
}
fclose($han);
}
sort($stack); //sort arr2
$newarray = array();
foreach($stack as $val){
$lineid = $val[0]; $segmentid = $val[1]; $action = $val[2];
$newarray[$lineid][$segmentid] = $action;
}
foreach($newarray as $value) {
foreach($value as $key => $value2){
echo $key . " | " . $value2 . "<br />";
}
echo "STOP";
}
?>

希望这是有道理的。

由于

1 个答案:

答案 0 :(得分:1)

循环逻辑有问题。如果您仍然要遍历每个“b”元素,为什么还要定义“a”元素的唯一性?无论如何,也许它只是我,但如果你仍然想按照你的假性方式去做,你可以: 1.将csv中的行转换为array1 2.将array1存储到另一个array2中 3.排序arraty2 4.循环遍历array2并跟踪第一个元素(元素a)

中的任何变化
$i=0;
$stack = array();
if (($han = fopen("3col.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($han, 50, ",")) !== FALSE) {
        array_push($stack, $data); //push each arr1 into arr2(stack)
        $i++;
        $num = count($data);
        echo "<p>";
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "|";
        }
        echo "</p>";
    }
    fclose($han);
}

sort($stack); //sort arr2

foreach($stack as $key => $arr2){
    $sameA = ($arr2[0] == $a) ? "" : "-->";
        echo $sameA;

    foreach($arr2 as $key => $val){ 
        echo $val." ";
    }
        $a = $arr2[0];
        echo "<br/>";
}

和输出:

-->1 | 123 | 0 | 
1 | 124 | 0 | 
1 | 125 | 0 | 
1 | 126 | 0 | 
1 | 127 | 0 | 
1 | 128 | 0 | 
1 | 129 | 0 | 
1 | 130 | 1 | 
1 | 131 | 1 | 
1 | 132 | 1 | 
1 | 133 | 1 | 
1 | 134 | 1 | 
1 | 135 | 1 | 
-->2 | 123 | 0 | 
2 | 124 | 0 | 
2 | 125 | 0 | 
2 | 126 | 1 | 
2 | 127 | 1 | 
2 | 128 | 1 | 
2 | 129 | 1 | 
2 | 130 | 1 | 
2 | 131 | 1 | 
2 | 132 | 1 | 
2 | 133 | 1 | 
2 | 134 | 1 | 
2 | 135 | 1 | 
-->3 | 256 | 0 | 
3 | 321 | 0 | 
3 | 456 | 0 | 
3 | 489 | 0 | 
3 | 652 | 1 | 
3 | 741 | 1 | 
3 | 965 | 0 | 

每次启动新的“a”元素时都可以看到箭头