开发者

How do I extract structured data from CSV dump in php

I am experimenting with loading CSV in PHP.

I have a CSV file, with 3 columns, simple values.

I have managed to be able to loop through each line, and output each value, but I need to grab data conditionally based on unique values and based on a certain position in the row.

For example, for each unique value in column a, I need to generate an array of data objects based on the value of column b, using column c as a conditional for one of the values.

My 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

Code I am using to interrogate:

<?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);
?>

Output

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 what I am wanting to achieve

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
}

EDIT SOLUTION:

<?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";
}
?>

Hope that makes sense.

Thanks


There is something wrong with your loop logic. If you still gonna iterate through every single "b" element why do you have to define the uniqueness of your "a" element? Anyway, maybe it is just me, but if you still want to do it the way your psuedo is you can: 1. Convert line from your csv into array1 2. Store that array1 into another array2 3. Sort arraty2 4. Loop through array2 and keep track of any changes in the first element (element 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/>";
}

and the Output:

-->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 | 

You can see an arrow every time you start new "a" element

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜