Home > Blockchain >  how to filter an array of rows from SQL results
how to filter an array of rows from SQL results

Time:03-29

when i print $results i get the array as below which i want to filter and use part of it for different output

    $stmt = $sqlselect->fetchAll(PDO::FETCH_ASSOC);
           $results = [];
           foreach ($stmt as $key => $value) {  
              $results[$key] = $value;
           }
print_r($results);
            Array
        (
            [0] => Array
                (
                    [id] => 2
                    [employee] => 1
                    [startdate] => 2022-02-01
                    [enddate] => 2022-02-28
                    [evaluatedby] => 
                    [evaluationdate] => 
                    [evaluation] => 
                    [submitted] => 0
                    [attachment] => 
                    [jobtitle] => Software Developer
                    [department] => Planning & Development
                    [knownas] => Mike
                )
        
            [1] => Array
                (
                    [id] => 2
                    [employee] => 1
                    [startdate] => 2022-02-01
                    [enddate] => 2022-02-28
                    [evaluatedby] => 
                    [evaluationdate] => 
                    [evaluation] => 
                    [submitted] => 0
                    [attachment] => 
                    [jobtitle] => Administrator
                    [department] => Accounts
                    [knownas] => Mike
                )
        )

i want to break/filter the $results to look like the following array so that i can use it on different part of my code.

    Array
(
    [0] => Array
        (
            [knownas] => mike
            [department] => Planning & Development
            [jobtitle] => Software Developer
            [id] => 2
            [startdate] => 2022-02-01
            [enddate] => 2022-02-28
        )

    [1] => Array
        (
            [knownas] => Mike
            [department] => Accounts
            [jobtitle] => Administrator
            [id] => 2
            [startdate] => 2022-02-01
            [enddate] => 2022-02-28
        )
)

i tried this.

 $array = ['name','department','jobtitle','id','startdate','enddate'];
         $filterarray[] = array_filter($results, function($key) use ($array)
         { 
            return in_array(array('name','department','jobtitle','id','startdate','enddate'),$key);
         });
         print_r(json_encode($filterarrays));

when i run it return an empty array. is the a way i can get around this.

CodePudding user response:

You have a few issues here.

First, if you are already passing an array as a variable, you can simply use that variable.

Second, in_array() takes the first variable as needle (the key in your case), and the second variable as haystack (the array in your case)

Third, you are missing ARRAY_FILTER_USE_KEY in your array_filter, so the function tries to filter based on the values, instead of the keys.

Your code should look like this:

 $array = ['name','department','jobtitle','id','startdate','enddate'];
     $filterarray[] = array_filter($results, function($key) use ($array)
     { 
        return in_array($key, $array);
     }, ARRAY_FILTER_USE_KEY);

CodePudding user response:

I think you might actually want to use array_map and then array_filter inside. See code example below, but also checkout the answer here for more details on both: https://stackoverflow.com/a/3432266/1428907

$newArray = array_map(static function (array $result) {
    return array_filter($result, fn($key) => in_array($key, ['name','department','jobtitle','id','startdate','enddate']), ARRAY_FILTER_USE_KEY);
}, $results);
  • Related