0

I want to iterate over a multidimensional array, count the occurrences of a String inside and delete Array items where the count is higher than e.g. 3.

I've already tried a pretty messy combination of array_search, array_count_values and strpos inside a N^N loop, but this takes way to long to process and the results are wrong...

This is the Array, I'm trying to alter

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"
  }
}

The surrounding Array can have around 200 Items. I'm looking for a way to remove Items if theyr productcategory is found more than X times.

Can you guys help me with this?

3
  • 1
    example of what u want to do please since I don't understand Dec 9, 2015 at 14:20
  • 1
    have you tried with the function array_walk_recursive ? php.net/manual/fr/function.array-walk-recursive.php Dec 9, 2015 at 14:31
  • I havent been able to use a one size fits it all approach, but for future reference i will share my "solution". It doesnt feel like super sophisiticated but it gets the job done... ... see below Dec 18, 2015 at 13:47

3 Answers 3

0

Yeah I've had to deal with something kind of similar. If you're looking at an array of around 200, then it should be too slow to create a counter loop and then unset the values of the original array based on those counters. I've provided a template to think about, to see if this is the direction you're after.

It makes a copy of the array, then counts the productcategory, of course I'm assuming that category^^subcategory is the count you are looking for.

<?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>';

I'm unsetting that item in the sub array, as I'm not sure if you want to just unset the whole item.

0

My first question for you would be "where is your data coming from?" If this is coming out of a database, then I would recommend you tweak your query there. You can definitely solve this in PHP, but as your data set grows it will take longer and longer to loop over the dataset in PHP.

To solve this in PHP, I would recommend you create a new "product index" array. This array would be associative with the product name as the keys and the values would contain an array of all the top-level indexes in your dataset array. Once you've built the index array, you can loop over that to find which product types occur more than 3 times in the main dataset and quickly delete those items.

$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]);
        }
    }
}
0

I havent been able to use a one size fits it all approach, but for future reference i will share my "solution". It doesnt feel like super sophisiticated but it gets the job done...

function filter_categories($input, $count) {

        $output = $input;

        $exploded_input = [];
        foreach ($output as $key => $value) {
            $exploded_items = explode("^^", $value["productcategory"]);
            array_push($exploded_input, $exploded_items);
        }
        $sortedbyCategory = [];

        $last_items = [];
        $counted_items = [];
        foreach ($exploded_input as $key => $value) {
            $end = end($value);
            array_push($last_items, $end);
        }

        $counted = array_count_values($last_items);

        foreach ($counted as $key => $value) {
            if($value<=$count) {
                unset($counted[$key]);
            }
        }
        foreach ($counted as $k => $v) {
            for ($i=0; $i < count($input); $i++) { 
                if(strpos($input[$i]["productcategory"], $k)){
                    if($counted[$k] > $count) {
                        $input[$i]["hide"] = true;
                        $counted[$k]--;
                    }

                }

            }
        }
        foreach ($input as $key => $value) {
            if(isset($value["hide"])) {
                unset($input[$key]);
            }
        }

        return $input;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.