检查数组的数组是否包含某个字符串


Check if an array of an array contains a certain string

在向父数组添加任何新的子数组之前,我正在检查以确保数组数组不包含某些字符串

我想确保如果存在具有相同websitecondition的数组,则不会将新的子数组添加到父数组中。

例如,在此示例中,$newArr不得插入数组$arr因为它们已经存在具有相同websitecondition的数组。

$arr = array(
   array(
      'website' => 'amazon',
      'price' => 20,
      'location' => 'uk',
      'link' => '...',
      'condition' => 'new'
   ),
   array(
      'website' => 'abe',
      'price' => 20,
      'location' => 'uk',
      'link' => '...',
      'condition' => 'new'
   )
);
$newArr = array(
      'website' => 'amazon',
      'price' => 60,
      'location' => 'uk',
      'link' => '...',
      'condition' => 'new'
   )

我正在寻找一个简单的解决方案,因为在父数组上使用函数in_array是不够的。

到目前为止的代码

$arr = array();
foreach($table->find('tr.result') as $row){
   if(($website = $row->find('a img',0)) 
      && ($price = $row->find('span.results-price a',0))
      && ($location = $row->find('.results-explanatory-text-Logo'))                     
      && ($link = $row->find('a',0))){                  
      $website = str_replace( array('.gif','.jpg','.png'), '', basename($website->src));
      $price = floatval(trim(str_replace(',', '', $price->innertext), "£")); 
      $location = "uk";         
      $link = $link->href;
      $arr[] = array(
         'website' => $website,
         'price' => $price,
         'location' => $location,
         'link' => $link,
         'condition' => 'new'
      );
   }            
}

每次都遍历$arr以查找$website$condition(总是'new'?),或者您可以保留找到的键的辅助数组。如果您每次都从空$arr开始,则第二种方法将起作用并且速度更快。

$arr = array();
$keys = array();
foreach($table->find('tr.result') as $row){
   if(...){                  
      ...
      $condition = 'new'; // set as needed
      // track seen keys
      $key = $website . '|' . $condition; // assumes neither field contains '|'
      if (!isset($keys[$key])) {
         $keys[$key] = true;
         $arr[] = array(...);
      }
   }            
}
我希望

下面代码中的注释不言自明......我不是PHP专业人士,这可能不是最优雅的方式,但我相信逻辑是有道理的。显然,$new_array 对象有一些未声明的变量,但它只是例如。

我希望这有所帮助,没有人投票反对我:)

<?php
    // Original array
    $arr = array();
    foreach($result as $row) {
        // Get the new array as an object first so we can check whether to add to the loop
        $new_array = array(
            'website' => $website,
            'price' => $price,
            'location' => $location,
            'link' => $link,
            'condition' => 'new'
        );
        // If the original array is empty there's no point in looping through it
        if(!empty($arr)) {
            foreach($arr as $child) {
                // Check through each item of the original array
                foreach($new_array as $compare) {
                    // Compare each item in the new array against the original array
                    if(in_array($compare, $child)) {
                        // if there's a match, the new array will not get added
                        continue;
                    }
                }
            }   
        }
            // If there's no match, the new array gets added
        $arr[] = $new_array;
    }
?>