Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查数组的数组是否包含特定字符串_Php_Arrays - Fatal编程技术网

Php 检查数组的数组是否包含特定字符串

Php 检查数组的数组是否包含特定字符串,php,arrays,Php,Arrays,在将任何新的子数组添加到父数组之前,我正在检查以确保数组数组数组不包含某些字符串 我想确保如果存在具有相同网站和条件的数组,则不会将新的子数组添加到父数组中 例如在本例中,$newArr不得插入数组$arr,因为它们已经存在一个具有相同网站和条件的数组 $arr = array( array( 'website' => 'amazon', 'price' => 20, 'location' => 'uk', 'link' =

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

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

例如在本例中,
$newArr
不得插入数组
$arr
,因为它们已经存在一个具有相同
网站和
条件的数组

$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'
   )
我正在寻找一个简单的解决方案,因为在父数组上使用函数
是不够的

到目前为止的代码

$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_数组对象有一些未声明的变量,但它只是一个示例

我希望这会有所帮助,也希望没有人反对我:)


看起来像是一个标准的搜索,并不优雅,但如果没有其他结果,我可能会使用它。您只比较值,因此映射到相同值的两个不同键将触发“匹配”,尽管考虑到每种类型的元素都非常不同,这不太可能。但更重要的是,
continue
在这里没有任何效果,因为它是每个
循环的最后一条语句。因此,
$new_数组
每次都将添加到
$arr
<?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;
    }
?>