Jump to content

PHP Filter multidimensional-array key with another array


Tony_146
Go to solution Solved by mac_gyver,

Recommended Posts

What I'm trying to do is read in a worksheet from an excel file and only keep certain columns based on a list in another array. I able to read in the excel sheet using PHPExcel, but haven't been successfully filtering out the unwanted column keys. I have been searching the web and been trying to apply the following: array_intersect_key, functions, array_filter and no success.

      <?php

    /** Include path **/
    set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');

    /** PHPExcel_IOFactory */
    include 'PHPExcel/IOFactory.php';


    //$inputFileType = 'Excel5';
    //  $inputFileType = 'Excel2007';
    //  $inputFileType = 'Excel2003XML';
    //  $inputFileType = 'OOCalc';
    //  $inputFileType = 'Gnumeric';
    $inputFileName = './sampleData/example1.xls';
    $sheetname = 'Visual';

    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    echo 'Loading Sheet "',$sheetname,'" only<br />';
    $objReader->setLoadSheetsOnly($sheetname);
    $objPHPExcel = $objReader->load($inputFileName);


    echo '<hr />';

    echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded<br /><br />';
    $loadedSheetNames = $objPHPExcel->getSheetNames();
    foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
        echo $sheetIndex,' -> ',$loadedSheetName,'<br />';
    }

    $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

    // Removes NULL values
    foreach($sheetData as $key => &$row) {
        $row = array_filter($row,function($cell){return !is_null($cell);});
        if (count($row) == 0) {
            unset($sheetData[$key]);
        }
    }
    unset ($row);

    unset ($sheetData[1]); // Remove row 1 of spreadsheet 
    unset ($sheetData[2]); // Remove row 2 of spreadsheet

    // columns what to keep, this is just example
    $col_keep = array('A', 'B', 'D', 'K');

    foreach($sheetData as $k=>$v) {
      foreach($v as $c=>$vb){
      echo "$c<br>"; // this display the columns keys e.g A,B,C,etc... 
      // How to filter here with array $col_keep?        
      }
    }

     ?>
Link to comment
Share on other sites

Here are the results:

Array ( [1] => Array ( [A] => DATE 5/13/13 [B] => [C] => [D] => [E] => [F] => [G] => [H] => [I] => [J] => [K] => [L] => [M] => [N] => [O] => ) [2] => Array ( [A] => Bunch of numbers [B] => [C] => [D] => [E] => [F] => [G] => [H] => [I] => [J] => [K] => [L] => [M] => [N] => [O] => ) [3] => Array ( [A] => cat [B] => dog [C] => snake [D] => rat [E] => pig [F] => cow [G] => sheep [H] => horse [I] => fish [J] => hamster [K] => bird [L] => goat [M] => chicken [N] => parrot [O] => shark ) [4] => Array ( [A] => 1 [B] => [C] => [D] => [E] => [F] => [G] => [H] => [I] => [J] => [K] => [L] => [M] => [N] => [O] => ) [5] => Array ( [A] => [B] => 1 [C] => 2 [D] => [E] => [F] => [G] => [H] => [I] => [J] => [K] => [L] => [M] => [N] => [O] => ) [6] => Array ( [A] => [B] => 3 [C] => 4 [D] => [E] => [F] => [G] => [H] => [I] => [J] => [K] => [L] => [M] => [N] => [O] => ) [7] => Array ( [A] => [B] => [C] => [D] => 1 [E] => 2 [F] => 3 [G] => [H] => [I] => [J] => [K] => [L] => [M] => [N] => [O] => ) [8] => Array ( [A] => [B] => [C] => [D] => 4 [E] => 5 [F] => 6 [G] => [H] => [I] => [J] => [K] => [L] => [M] => [N] => [O] => ) [9] => Array ( [A] => [B] => [C] => [D] => 7 [E] => 8 [F] => 9 [G] => [H] => [I] => [J] => [K] => [L] => [M] => [N] => [O] => ) [10] => Array ( [A] => [B] => [C] => [D] => [E] => [F] => [G] => 1 [H] => 2 [I] => 3 [J] => 4 [K] => [L] => [M] => [N] => [O] => ) [11] => Array ( [A] => [B] => [C] => [D] => [E] => [F] => [G] => 5 [H] => 6 [I] => 7 [J] => 8 [K] => [L] => [M] => [N] => [O] => ) [12] => Array ( [A] => [B] => [C] => [D] => [E] => [F] => [G] => 9 [H] => 10 [I] => 11 [J] => 12 [K] => [L] => [M] => [N] => [O] => ) [13] => Array ( [A] => [B] => [C] => [D] => [E] => [F] => [G] => 13 [H] => 14 [I] => 15 [J] => 16 [K] => [L] => [M] => [N] => [O] => ) [14] => Array ( [A] => [B] => [C] => [D] => [E] => [F] => [G] => [H] => [I] => [J] => [K] => 1 [L] => 2 [M] => 3 [N] => 4 [O] => 5 ) [15] => Array ( [A] => [B] => [C] => [D] => [E] => [F] => [G] => [H] => [I] => [J] => [K] => 6 [L] => 7 [M] => 8 [N] => 9 [O] => 10 ) [16] => Array ( [A] => [B] => [C] => [D] => [E] => [F] => [G] => [H] => [I] => [J] => [K] => 11 [L] => 12 [M] => 13 [N] => 14 [O] => 15 ) [17] => Array ( [A] => [B] => [C] => [D] => [E] => [F] => [G] => [H] => [I] => [J] => [K] => 16 [L] => 17 [M] => 18 [N] => 19 [O] => 20 ) [18] => Array ( [A] => [B] => [C] => [D] => [E] => [F] => [G] => [H] => [I] => [J] => [K] => 21 [L] => 22 [M] => 23 [N] => 24 [O] => 25 ) ) 
Edited by Tony_146
Link to comment
Share on other sites

  • Solution

i'm not sure how you are going to use this data, but assuming an array that only has the $col_keep columns left in it will work, try this -

$col_keep = array_flip(array('A', 'B', 'D', 'K')); // make these values into the array keys

foreach($sheetData as $k=>$v) { // $v is the array you want to filter
    $keep = array_intersect_key($v,$col_keep); // intersect using the keys
    print_r($keep);
}
Edited by mac_gyver
Link to comment
Share on other sites

 

i'm not sure how you are going to use this data, but assuming an array that only has the $col_keep columns left in it will work, try this -

$col_keep = array_flip(array('A', 'B', 'D', 'K')); // make these values into the array keys

foreach($sheetData as $k=>$v) { // $v is the array you want to filter
    $keep = array_intersect_key($v,$col_keep); // intersect using the keys
    print_r($keep);
}

Thanks mac_gyver!!!! Seem simple, think I was overthinking it with the multidimensional array and couldn't get the right combo in the code to work.  Thanks again!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.