Search code examples
phpforeachfgetcsv

How to exclude keys from a php multidimensional array?


I have a code that allows me to read a csv file like this:

REFERENCE;COLOR;QUANTITY;TURNOVER;SELL TROUGH;COMMENT
GJK0C9;8952;3;90;3%;Pack S
GJKCS4;399;2;19;10%;Windows

And that allows me to browse through it and display the csv information on a page like this:

enter image description here

But I would like the first two columns, "REFERENCE" and "COLOR" to appear only at the top of each image, not with the inputs.

How can I exclude them from the display of my array?

<?php

$column = []; //create an array

        if (($handle = fopen("$nomcsv", "r")) !== FALSE) { 

                        // Get headers
                if (($data = fgetcsv($handle, 1000, ';')) !== FALSE)
                {
                    $column = $data; // assign header value to array
                }

                 // Get the rest
            while(($data = fgetcsv($handle, 1000000, ";")) !== FALSE) {

                    $file = '//Alcyons/it/PhotoShoot/Photos_Outil/'.$data[0].'_'.str_pad(trim($data[1]),4, "0", STR_PAD_LEFT).'-1.jpg';
                    $type = pathinfo($file, PATHINFO_EXTENSION);

                    echo "<tr><td valign=top><strong>".$data[0]."</strong><br><font color=\"#666666\">Color:</font>".$data[1]."
                    <div style=\"font-size:10px;\"><br>RRP:
                    <br><input type=text value=\"".$rrp."\" style=\"width:100px\">";

                    $row = array_combine($column,$data); // combine header with values
                    foreach($row as $key=>$value){

                        array_splice($key, 1, 1); //I tried this but doesn't work
                        echo "<br>$key:<br><input type=text value=\"".$value."\" style=\"width:100px\">";
                    } 

                    echo "</td><td valign=top align=center>";?><img src="<?php echo "data:image/$type;base64,",base64_encode(file_get_contents($file))?>""
                    <?php
                    echo "border=0 width=180></a></td><td width=10></td>";

                  }     

                }

?>

Solution

  • you need to apply if to check key and skip printing those 2 keys(REFERENCE and COLOR):

    foreach($row as $key=>$value){
    
      if($key !=='REFERENCE' && $key !== 'COLOR'){
        echo "<br>$key:<br><input type=text value=\"".$value."\" style=\"width:100px\">";
      }
    }