0

OK, so I have : an array(a) of arrays(b) of arrays(c).

I'm trying to iterate through the array and unset (or to be precise delete) all arrays at level 'c', with less than 3 items.

How would you go about it?

I've tried every possible use of unset but I still can't the result I need.

foreach ($data as $a=>&$data_section)
{
    foreach ((array)$data_section as $b=>$pattern)
    {
        if (count((array)$pattern)<3) { unset($data_section[$b]); }
    }
}

This one gives an error :

Fatal error: Cannot unset string offsets

4

1 回答 1

0

为什么不直接使用array_filter

php 5.3+ 语法

$data = array_filter($data, function($a){ return count($a) >2; });

预 PHP 5.3

function countGreaterThanTwo($a){ return count($a) >2; };
$data = array_filter($data, "countGreaterThanTwo");

所以在你上面的例子中你会做

foreach ($data as $a=>&$data_section)
{
    foreach ($data_section as $b=>&$pattern)
    {
        $pattern = array_filter($pattern, function($a){ return count($a) >2; });
    }
}
于 2013-07-18T19:52:17.417 回答