gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:47:39 25 4
gpt4 key购买 nike

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

最佳答案

我想出了以下替代方案:

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

关于php - 如果找到第一个数组中的特定键,则在第二个数组中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54298488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com