1

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

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

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

$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'
      );
   }            
}
4

2 回答 2

1

我希望下面代码中的注释能说明一切......我不是 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;
    }
?>
于 2012-08-11T01:22:10.877 回答
1

$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(...);
      }
   }            
}
于 2012-08-11T01:18:29.507 回答