【问题标题】:Finding values in second array if specific key from first array is found如果找到第一个数组中的特定键,则在第二个数组中查找值
【发布时间】:2019-06-15 08:18:17
【问题描述】:

我编写了一个代码,如果它从第一个数组中找到特定键,则在第二个数组中找到值,但我的问题是 - 是否可以做得更好?例如没有 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].
                    ''
                    ;}
                }
            }

        }
    }

是否有可能做得更好/更好的代码性能/速度,因为我不知道当用户过滤时这 3 个循环是否会很好。 10000 种产品。

【问题讨论】:

  • !== FALSE after array_key_exists 是多余的。

标签: php arrays loops foreach


【解决方案1】:

我想出了以下替代方案:

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 with arguments

【讨论】:

    猜你喜欢
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2015-07-23
    • 1970-01-01
    • 2022-10-13
    相关资源
    最近更新 更多