如果在第一个数组中找到特定键,则从第二个数组中查找值

3
我已经编写了一段代码,如果在第一个数组中找到特定的键,则会在第二个数组中查找该键对应的值,但我的问题是 - 是否有更好的方法?例如,不使用3个循环?
例如,这里是要搜索的键和值,用户在表单中勾选并提交的($tegoszukamy):
array (
  'kolor' => 
     array (
       0 => 'bialy',
       1 => 'zielony',
  ),
  'rozmiar' => 
     array (
       0 => '60',
       1 => '70',
  ),
  'rozdzielczość' => 
     array (
       0 => '1200x1800',
  ),
  'moc' => 
     array (
       0 => '500W',
  ),
);

以下是一个包含产品ID的数组,用于进行搜索操作($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',
  )
)

以下是我的代码,它能正常运行:

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].
                    ''
                    ;}
                }
            }

        }
    }

是否有可能通过改进代码性能/速度来更好地实现它,因为我不知道当用户筛选10000个产品时,这3个循环是否足够好。


array_key_exists 后面加上 !== FALSE 是多余的。 - Dharman
1个回答

0
我想到了以下的替代方案:

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

我不确定它们中的任何一个是否比你的更快。我会把测试留给你。:)

第一个灵感来自于jensgram对这个问题的回答:PHP带参数的array_filter


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接