1

親配列に新しい子配列を追加する前に、配列の配列に特定の文字列が含まれていないことを確認しています

website同じ配列がcondition存在する場合、新しい子配列が親配列に追加されないことを確認したいと思います。

たとえば、この例では、とが同じ配列がすでに存在するため、$newArrを配列に挿入しないでください。$arrwebsitecondition

$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

$arr毎回ループし$website$condition(常に'new'?)を検索するか、見つかったキーの2次配列を保持できます。$arr毎回空から始める場合は、2番目のアプローチが機能し、より高速になります。

$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 に答える
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 に答える