3

I have written a code, that finds value in second array if it finds specific key from first array, but my question is - is it possible to do it better? for example without 3 loops ?

For example here are keys and values to search for, which user has checked at form and submitted ($tegoszukamy):

array (
  'kolor' => 
     array (
       0 => 'bialy',
       1 => 'zielony',
  ),
  'rozmiar' => 
     array (
       0 => '60',
       1 => '70',
  ),
  'rozdzielczość' => 
     array (
       0 => '1200x1800',
  ),
  'moc' => 
     array (
       0 => '500W',
  ),
);

Here is array with products IDs where searching is performed ($tuszukamy):

array (
  47 => 
    array (
      'rozmiar' => '50,60,70,80,90,100',
      'kolor' => 'bialy,czarny',
  ),
  48 => 
    array (
      'rozmiar' => 'L,M,XS,S,L',
      'kolor' => 'zielony,niebieski,czerwony,zolty,bialy,czarny',
  ),
  49 => 
    array (
      'rozdzielczość' => '1200x1800',
      'prędkość' => '60str/min',
  )
)

Here is my code that is working fine:

foreach ($tegoszukamy as $atrybut=>$wartosci_szukane) {
    foreach ($tuszukamy as $numer_posta=>$wartosci_zbioru ) {

        if (array_key_exists($atrybut, $wartosci_zbioru) !== FALSE){

            foreach ($wartosci_szukane as $ws) {
                if (strpos($wartosci_zbioru[$atrybut],$ws) !== FALSE) {
                    echo 
                        'We have found'
                        .$ws.
                        'in'
                        .$wartosci_zbioru[$atrybut].
                        'where product id is'
                        .$numer_posta.
                        ''
                        ;}                      
                else {
                    echo 
                    'We found not'
                    .$ws.
                    'in'
                    .$wartosci_zbioru[$atrybut].
                    ''
                    ;}
                }
            }

        }
    }

Is it possible to do it better/with better code performance/speed, because I dont know if these 3 loops will be good when user filters through eg. 10000 products.

1
  • !== FALSE after array_key_exists is redundant. Commented Jan 21, 2019 at 22:22

1 Answer 1

0

I came up with the following alternatives:

1.

class Subject {
private $attr_name;
private $attr_values;

function __construct($attr_name, $attr_values) {
    $this->attr_name = $attr_name;
    $this->attr_values = $attr_values;
}

public function check($key, $item) {
    $found = array();

    if (isset($item[$this->attr_name])) {
        foreach($this->attr_values as $val) {
            strstr($item[$this->attr_name], $val) && array_push($found, $val);
        }
    }

    count($found) > 0 ? 
        $message = "Found attribute <u>" . $this->attr_name . "</u> with value <b>" . implode(", ", $found) . "</b> in ID: " . $key . "."
        : 
        $message = "No matches for <u>" . $this->attr_name . "</u> found in ID: " . $key;

    return $message;
}
}

foreach ($tegoszukamy as $attr_name=>$attr_values) {
$filtered = array_map(array(new Subject($attr_name, $attr_values), "check"), array_keys($tuszukamy), $tuszukamy);
foreach($filtered as $result) {
    echo $result . '<br>';
}
}

2.

foreach ($tegoszukamy as $attr_name=>$attr_values) {
    $filtered = array_filter($tuszukamy, function ($item, $key) use($attr_name, $attr_values) {
    $found = array();

    if (isset($item[$attr_name])) {
        // var_dump($item[$attr_name]);
        foreach($attr_values as $val) {
            strstr($item[$attr_name], $val) && array_push($found, $val);
        }
    }

    count($found) > 0 ? 
    $message = "Found attribute <u>" . $attr_name . "</u> with value <b>" . implode(", ", $found) . "</b> in ID: " . $key . "."
    : 
    $message = "No matches for <u>" . $attr_name . "</u> found in ID: " . $key;

    echo $message . "<br>";

    return count($found) > 0;

}, ARRAY_FILTER_USE_BOTH);

// something to do with $filtered;
}

I'm not sure whether either of them is faster than yours. I'll leave the testing to you. :)

The first one was inspired by jensgram's answer to this question: PHP array_filter with arguments

Sign up to request clarification or add additional context in comments.

Comments

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.