PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, March 7, 2022

[FIXED] check index of columns in key of an array and remove columns with value 0

 March 07, 2022     arrays, csv, php     No comments   

Issue

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?


Solution

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)
        ];
    }
}


Answered By - Don't Panic
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing