Home > other >  check index of columns in key of an array and remove columns with value 0
check index of columns in key of an array and remove columns with value 0

Time:03-01

I have a php code that takes the matched rows of a csv file and puts them in an array. my csv file looks like this:

Company,Produkt,Sortiment name,31,32,33,34,35,36,37,38 //these are shoe sizes
Dockers,AD1234,Sort A,2,3,5,3,2,1,0,0  //and these numbers are how many pairs of shoes
Addidas,AB1234,Sort B,2,2,1,4,,0,0,4,3
Nike,AC1234,Sort C,0,2,0,1,4,0,4,3
Dockers,AE1234,Sort D,0,1,2,3,4,1,0,2

and my php code is

$csv = file_get_contents($_SERVER['DOCUMENT_ROOT'] . 'CsvTest/Sortiment.csv');

    $input = 'Company'; // column
    $value = 'Dockers'; // what value of that column

    $csv = array_map("str_getcsv", explode(PHP_EOL, $csv));
    $keys = array_shift($csv);
    $key = array_search($input, $keys);

    $sortiment_array = array();
    while ($line = array_shift($csv)) {
        if ($line[$key] == $value) {
            $line = implode(',', $line) . PHP_EOL;
            $sortiment_array[] = $line;
        }
    }

so var_dump($sortiment_array); will give me the following

array(2) {
  [0]=>
  string(39) "Dockers,AD1234,Sort A,2,3,5,3,2,1,0,0"
  [1]=>
  string(39) "Dockers,AE1234,Sort D,0,1,2,3,4,1,0,2"
}

What I would like to do is to have the 0 columns taken out from the array and so therefore I need to identify what pair of shoes was not 0 ? So I need the first row (which is the header for my case) to repeat itself for each key and take out the shoe size that had 0 pairs. basically my array should turn into something like:

array(2) {
  [0]=>array(2)
       ['shoe size']=> "Producer,Produkt,Sortiment name,31,32,33,34,35,36" // no 37,38 
       ['sortiment']=> "Dockers,AD1234,Sort A,2,3,5,3,2,1,"// no 0
  [1]=>array(2)
       ['shoe size']=> "Producer,Produkt,Sortiment name,32,33,34,35,36,38" // no 31, 37 
       ['sortiment']=> "Dockers,AE1234,Sort D,1,2,3,4,1,2" 
}

Basically in 'shoe size' sizes should be taken out where the matched row has 0 pairs for that size. I hope I can explain it. I tried my best. Any suggestions?

CodePudding user response:

If all the rows in the data are the same size, you can combine the keys and values for each line that matches, then filter that to remove the zeros.

while ($line = array_shift($csv)) {
    if ($line[$key] == $value) {

        // combine keys and values, and filter to remove zeros
        $filtered = array_filter(array_combine($keys, $line));

        // separate the resulting keys and values and add them to your output array
        $sortiment_array[] = [
            'shoe size' => implode(',', array_keys($filtered)),
            'sortiment' => implode(',', $filtered)
        ];
    }
}

CodePudding user response:

<?php

$csv = file_get_contents($_SERVER['DOCUMENT_ROOT'] . 'CsvTest/Sortiment.csv');

$input = 'Company'; // column
$value = 'Dockers'; // what value of that column

$csv = array_map("str_getcsv", explode(PHP_EOL, $csv));

$keys = array_shift($csv);
$key = array_search($input, $keys);

$sortiment_array = array();
while ($line = array_shift($csv)) {
    if ($line[$key] == $value) {
        $lineStr = implode(',', $line) . PHP_EOL;

        $outputKeys = [];
        $outputLine = [];

        // Look through $line to find non-'0' elements and for each of them,
        // add the corresponding elements to $outputKeys and $outputLine:
        for( $i=0; $i < sizeof($keys); $i   ) {
            if ( $line[$i] !== '0' ) { // No '0' in this slot so add this slot to $outputKeys and $outputLine:
                $outputKeys[] = $keys[$i];
                $outputLine[] = $line[$i];
            }
        }

        // Join $outputKeys and $outputLines back into a string:
        $sortiment_array[] = [
            join(',', $outputKeys),
            join(',', $outputLine)
        ];
    }
}
print_r($sortiment_array);

CodePudding user response:

You can implement the logic which does it for a pair of arrays, the first being the template (header row) and the second the csv row after the header.

function nonZeros($template, $row) {
    $output = [
        'shoe_size' => [],
        'sortiment' => []
    ];
    for ($index = 0; $index < count($row); $index  ) {
        if ($row != 0) {
            $output['shoe_size'][]=$template[$index];
            $output['sortiment'][]=$row[$index]
        }
    }
    return $output;
}

and then you can loop the lines and call nonZeros, passing the corresponding arrays.

  • Related