简体   繁体   English

如何根据多个选定值过滤数组

[英]How to filter an array based on multiple selected values

I have an array containing multiple values including name, code GPU, CPU, HDD, RAM ect:我有一个包含多个值的数组,包括名称、代码 GPU、CPU、HDD、RAM 等:

Array
(
    [0] => Array
        (
            [code] => 000001
            [name] => Lenovo P1
            [brand] => Lenovo
            [GPU] => RTX 3070
            [CPU] => i7
            [HDD] => 1 TB
            [RAM] => 32GB
            [screen] => 4k
        )

    [1] => Array
        (
            [code] => 000002
            [name] => Lenovo P1
            [brand] => Lenovo
            [GPU] => RTX 3070
            [CPU] => i5
            [HDD] => 1 TB
            [RAM] => 16GB
            [screen] => FHD
        )

    [2] => Array
        (
            [code] => 000003
            [name] => HP ZBook 16
            [brand] => HP
            [GPU] => RTX A2000
            [CPU] => i7
            [HDD] => 1 TB
            [RAM] => 32GB
            [screen] => FHD
        )
);

I need to filter the array and display only those rowsets based on type selected by $_GET value.我需要过滤数组并根据 $_GET 值选择的类型仅显示那些行集。

if $_GET['CPU'] = 'i7';如果$_GET['CPU'] = 'i7'; is selected show everything with $row[CPU] = 'i7';被选中 show everything with $row[CPU] = 'i7';

if $_GET['CPU'] = 'i7';如果$_GET['CPU'] = 'i7'; and $_GET['GPU'] = 'RTX 3070';$_GET['GPU'] = 'RTX 3070'; is selected show everything with $row[CPU] = 'i7';被选中 show everything with $row[CPU] = 'i7'; and $row[GPU] = 'RTX 3070';$row[GPU] = 'RTX 3070';

if $_GET['CPU'] = 'i7';如果$_GET['CPU'] = 'i7'; and $_GET['GPU'] = 'RTX 3070';$_GET['GPU'] = 'RTX 3070'; and $_GET['RAM'] = '16GB';$_GET['RAM'] = '16GB'; is selected show everything with $row[CPU] = 'i7';被选中 show everything with $row[CPU] = 'i7'; and $row[GPU] = 'RTX 3070';$row[GPU] = 'RTX 3070'; and $row[RAM] = '16GB';$row[RAM] = '16GB';

I could do this with simple if else statements inside foreach loop if it was one or two filters, but sometimes it might be one and sometimes it might be five or more filters (GPU, CPU, HDD, RAM, screen).如果它是一个或两个过滤器,我可以在 foreach 循环中使用简单的 if else 语句来做到这一点,但有时它可能是一个,有时它可能是五个或更多过滤器(GPU、CPU、HDD、RAM、屏幕)。

Simple example with only one filter:只有一个过滤器的简单示例:

        $cpu = $_GET['CPU'];
        
        $filtered_array = array();
        
        foreach ($array as $key => $value) {
        
          if ($value['CPU'] == $cpu) {
              $filtered_array[] =  array(
              'code'   => $value['code'], 
              'name'   => $value['name'], 
              'brand'  => $value['brand'],
              'GPU'    => $value['GPU'],
              'CPU'    => $value['CPU'],
              'HDD'    => $value['HDD'],
              'RAM'    => $value['RAM'],
              'screen' => $value['screen']);
          }
}
    //print array with filtered CPUs from GET value
    print_r($filtered_array); 

This might be what you are looking for:这可能是您正在寻找的:

<?php
$search = [ 'GPU' => "RTX 3070",  'RAM' => "16GB" ];
$input = [
    [ 'GPU' => "RTX 3070", 'CPU' => "i7", 'RAM' => "32GB" ], 
    [ 'GPU' => "RTX 3070",  'CPU' => "i5", 'RAM' => "16GB" ],
    [ 'GPU' => "RTX A2000", 'CPU' => "i7", 'RAM' => "32GB" ],
];
$output = array_filter($input, function($entry) use($search) {
    foreach($search as $key => $val) {
        if (array_key_exists($key, $entry) && $entry[$key] != $val) return false;
    }
    return true;
});
print_r($output);

The output obviously is: output 显然是:

Array
(
    [1] => Array
        (
            [GPU] => RTX 3070
            [CPU] => i5
            [RAM] => 16GB
        )
)

I managed to do it modfying answer given by arkasha我设法修改了 arkasha 给出的答案

if (isset($_GET['search'])) {
  $search = array();
if (isset($_GET['GPU'])) {
  $search['GPU'] = $_GET['GPU'];
}
if (isset($_GET['RAM'])) {      
  $search['RAM'] = $_GET['RAM'];
}
if (isset($_GET['HDD'])) {      
  $search['HDD'] = $_GET['HDD'];
}
if (isset($_GET['Touch'])) {      
  $search['Touch'] = $_GET['Touch'];
}

  $input = $first_array;

$output = array_filter($input, function($entry) use($search) {
    foreach($search as $key => $val) {
        if (array_key_exists($key, $entry) && $entry[$key] != $val) return false;
    }
    return true;
});
}
else
{
  $output = $first_array;
}

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

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