简体   繁体   English

如何从 SQL 结果中过滤行数组

[英]how to filter an array of rows from SQL results

when i print $results i get the array as below which i want to filter and use part of it for different output当我打印 $results 时,我得到如下数组,我想过滤并将其中的一部分用于不同的 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.我想打破/过滤 $results 看起来像下面的数组,这样我就可以在我的代码的不同部分使用它。

    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.是我可以解决这个问题的方法。

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)其次, in_array()将第一个变量作为 needle (在您的情况下是关键),将第二个变量作为 haystack (在您的情况下是数组)

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.第三,您在array_filter中缺少ARRAY_FILTER_USE_KEY ,因此 function 尝试根据值而不是键进行过滤。

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

I think you might actually want to use array_map and then array_filter inside.我想你可能真的想在里面使用array_maparray_filter See code example below, but also checkout the answer here for more details on both: https://stackoverflow.com/a/3432266/1428907请参阅下面的代码示例,但也可以在此处查看答案以获取有关两者的更多详细信息: 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM