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);
2
Answers
I managed to do it modfying answer given by arkasha
This might be what you are looking for:
The output obviously is: