0

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
  • I want/I need is not a question. It just informs us that you want us to do the heavy lifting for you. Where are you stuck? What have you researched? What have you tried? To be clear, we'll help you at stackoverflow but we're not a free do-my-thinking service See how to ask and Minimal, Complete and Verifiable Example Commented Jan 29, 2023 at 15:27
  • I updated question, I also found kida similar solution here stackoverflow.com/questions/33881698/… but the problem is, I cannot use databse for this, I need to work with array I have Commented Jan 29, 2023 at 15:30

2 Answers 2

1

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
        )
)
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this works perfectly when I know how many filters there is selected and I put them into $search array, but filter count can be random ,it might be only GPU, or it might be GPU, CPU, screen ect. I guess I need to somehow create $search array according to what and how many _GET variables it is set. I could do huge if elseif statement and fill in $seach array like this, but I'm pretty sure there should be a lot smarter way to do it.
Sounds like that is a completely different task you face: not how to filter the data but how to actually create your filter clause selection. There is nothing I can add to that since you did not mention how or where you get that data from. I would expect that to be something a client transmits as part of a search form. If so, then why can't you simply define the search input fields as array elements and then filter that array on the server side so that only non empty entries remain?
0

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

2 Comments

This indeed is horrible, just as you yourself hinted. I would suggest that you change the form you probably use so that it contains input fields like that one: <input type="text" name=search[GPU]>. That will result in an array $_GET['search']['GPU'] and similar on the server side. Which is exactly the filter set you need.
Then all you need to do is filter the resulting set to only contain non empty calues, or you extend the solution I posted to also ignore entries in the set with empty values, just like it ignores keys in the set that are not present in the actual input data. So something like if (array_key_exists($key, $entry) && !empty($val) && $entry[$key] != $val) return false; ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.