0

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.

RayMal
  • 9
  • 1
  • 3

2 Answers2

0

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);
tola
  • 152
  • 1
  • 7
  • i changed all that, but its not filtering the array when i print $filterarray its not filtered it returns everything. – RayMal Mar 28 '22 at 09:52
  • @RayMal you can see my code running correctly [here](https://sandbox.onlinephpfunctions.com?s=jZJRa8IwFIXfBf_DHcjaQobap4FzQ1gHYxsM3R5EpER7ndGalDRVyth_N02rlbngQh9C7nfOvU3O3UOyTJqNdhsCrlBCLjIJcxEhLFEiAeQrkV81Gy2JaRarFPowkJLmzQb8Wu75UbEcFjnQvwefWOq4SWKRIxqqa6NSRaWKqCoxx-_4_k1Hf13H6sujc96_tfNbGmdaEM3yUnOJZILXDf4BXwDTbLZhSvc3XMeGUaXofLlBri74rcRMMRVX843EQu2oRHjELcYiQWkVRpjou647vMeUc8a_4PogNjWbfM3FjtO01L6xNf5Fej0dKVoESQdqctSQ0-ak_gVSpIichIAcn3faq9xbCxbrBBvTyVTbml1YnrqH_BJYZHxePIfbWmPuQZYiuOUoXuX0DfXAElUmOTAeGsSICFT8ofUPgcFwOBiHT8-vH8Ew_BwF4UswPtYTybgK9RAnI3q9PQ%2C%2C&v=8.1.3) – tola Mar 28 '22 at 10:02
  • it returns everything that's in the [0]=>array but not filtered. – RayMal Mar 28 '22 at 11:13
0

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);
ChristianM
  • 1,793
  • 12
  • 23