从数组中删除整数低于3的所有元素

时间:2013-09-21 20:43:30

标签: php arrays filter

我需要删除FacetValueCount低于3的所有元素。

我该怎么做?

这是我的数组:数组名称为$farben

array(8) {
  [0]=>
  array(2) {
    ["FacetValueName"]=>
    string(4) "Blau"
    ["FacetValueCount"]=>
    int(5)
  }
  [1]=>
  array(2) {
    ["FacetValueName"]=>
    string(7) "Schwarz"
    ["FacetValueCount"]=>
    int(3)
  }
  [2]=>
  array(2) {
    ["FacetValueName"]=>
    string(4) "blue"
    ["FacetValueCount"]=>
    int(2)
  }
  [3]=>
  array(2) {
    ["FacetValueName"]=>
    string(4) "Grau"
    ["FacetValueCount"]=>
    int(1)
  }
}

2 个答案:

答案 0 :(得分:2)

<?php

$farben = ARRAY();
$farben[] = array('FacetValueName'=>'Blau',  'FacetValueCount' => 5);
$farben[] = array('FacetValueName'=>'Schwarz',  'FacetValueCount' => 3);
$farben[] = array('FacetValueName'=>'blue',  'FacetValueCount' => 2);
$farben[] = array('FacetValueName'=>'Grau',  'FacetValueCount' => 1);

print '<pre>'; var_dump($farben); print '</pre>';

foreach ($farben AS $key => $row) {
  if ($row['FacetValueCount'] < 3) { unset($farben[$key]); }
}

print '<pre>'; var_dump($farben); print '</pre>';

?>

答案 1 :(得分:-2)

试试这个......

    $farben = array_filter($farben, function($row) { 
if($row["FacetValueCount"] > 3) {
    return $row;
}
});