Home > database >  How to filter an array based on multiple selected values
How to filter an array based on multiple selected values

Time:01-30

I have an array containing multiple values including name, code GPU, CPU, HDD, RAM ect:

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.

if $_GET['CPU'] = 'i7'; is selected show everything with $row[CPU] = 'i7';

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

if $_GET['CPU'] = 'i7'; and $_GET['GPU'] = 'RTX 3070'; and $_GET['RAM'] = '16GB'; is selected show everything with $row[CPU] = 'i7'; and $row[GPU] = 'RTX 3070'; and $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).

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

CodePudding user response:

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:

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

CodePudding user response:

I managed to do it modfying answer given by 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;
}
  • Related