0

多次元配列を反復処理し、内部の文字列の出現をカウントし、カウントがたとえば 3 より大きい配列アイテムを削除します。

N^N ループ内で array_search、array_count_values、および strpos のかなり厄介な組み合わせを既に試しましたが、これは処理に時間がかかり、結果が間違っています...

これは配列です。変更しようとしています

array(2) {
  [0]=>
  array(13) {
    ["id"]=>
    string(6) "1234"
    ["name"]=>
    string(28) "aa"
    ["productcategory"]=>
    string(30) "Branch1^^subbranch1"
    ["streamID"]=>
    int(0)
    ["streamContext"]=>
    string(16) "static"
    ["prio"]=>
    string(3) "100"
  }
  [1]=>
  array(11) {
    ["id"]=>
    string(6) "9876"
    ["name"]=>
    string(30) "bb"
    ["productcategory"]=>
    string(66) "Branch1^^subbranch2"
    ["streamID"]=>
    int(0)
    ["streamContext"]=>
    string(16) "static"
    ["prio"]=>
    string(3) "100"
  }
}

周囲の配列には、約 200 個のアイテムを含めることができます。製品カテゴリが X 回以上見つかった場合にアイテムを削除する方法を探しています。

これで私を助けてもらえますか?

4

3 に答える 3

0

ええ、私は似たようなものに対処しなければなりませんでした。約 200 の配列を見ている場合、カウンター ループを作成し、それらのカウンターに基づいて元の配列の値を設定解除するには遅すぎるはずです。これがあなたの求めている方向性であるかどうかを確認するために、考えるためのテンプレートを提供しました。

配列のコピーを作成し、 をカウントします。もちろん、それが探しているカウントであるとproductcategory想定しています。category^^subcategory

<?php

$your_array = array(
    array(
        array(
                "id" => "1234",
                "name" => "aa",
                "productcategory" => "Branch1^^subbranch1",
                "streamID" => '',
                "streamContext" => "static",
                "prio" => "100",
        ),
        array(
                "id" => "9876",
                "name" => "bb",
                "productcategory" => "Branch1^^subbranch1",
                "streamID" => '',
                "streamContext" => "static",
                "prio" => "100",
        ),
        array(
                "id" => "9876",
                "name" => "bb",
                "productcategory" => "Branch1^^subbranch3",
                "streamID" => '',
                "streamContext" => "static",
                "prio" => "100",
        ),
        array(
                "id" => "9876",
                "name" => "bb",
                "productcategory" => "Branch1^^subbranch2",
                "streamID" => '',
                "streamContext" => "static",
                "prio" => "100",
        ),
        array(
                "id" => "9876",
                "name" => "bb",
                "productcategory" => "Branch1^^subbranch3",
                "streamID" => '',
                "streamContext" => "static",
                "prio" => "100",
        ),
        array(
                "id" => "9876",
                "name" => "bb",
                "productcategory" => "Branch1^^subbranch1",
                "streamID" => '',
                "streamContext" => "static",
                "prio" => "100",
        ),
    ),
);




$counters = array();
$limit = 1; // whatever the limit is that you want
foreach ($your_array as $index => $array) {
    for ($i = 0; $i < count($array); $i++) {
        if (!isSet($counters[$array[$i]['productcategory']])) {
            $counters[$array[$i]['productcategory']] = 0;
        }
        $counters[$array[$i]['productcategory']]++;
        if ($counters[$array[$i]['productcategory']] > $limit) {
            unset($your_array[$index][$i]);
        }
    }
}

print '<pre>' . print_r($counters, true) . '</pre>';
print '<pre>' . print_r($your_array, true) . '</pre>';

アイテム全体の設定を解除したいかどうかわからないので、サブ配列でそのアイテムの設定を解除しています。

于 2015-12-09T16:29:46.043 に答える
0

あなたへの最初の質問は、「あなたのデータはどこから来たのですか?」です。これがデータベースからのものである場合は、そこでクエリを微調整することをお勧めします。これは PHP で確実に解決できますが、データセットが大きくなるにつれて、PHP でデータセットをループするのに時間がかかります。

PHP でこれを解決するには、新しい「製品インデックス」配列を作成することをお勧めします。この配列はキーとして製品名に関連付けられ、値にはデータセット配列内のすべての最上位インデックスの配列が含まれます。インデックス配列を構築したら、それをループして、メイン データセットで 3 回以上出現する製品タイプを見つけ、それらの項目をすばやく削除できます。

$productIndex = [];

// Build an index of product categories
foreach($dataset as $i => $row) {
   if (!is_array($productIndex[$row['productcategory']]) {
       $productIndex[$row['productcategory']] = [];
   }
   $productIndex[$row['productcategory']][] = $i;
}

// Search for indexes with > 3 rows
foreach($productIndex as $items) {
    if (count($items) > 3) {
        // Delete said rows
        foreach ($items as $index) {
            unset($dataset[$index]);
        }
    }
}
于 2015-12-09T16:50:47.497 に答える