什么是在PHP中过滤多维数组下面的最佳方法

时间:2014-02-26 16:22:02

标签: php arrays multidimensional-array

我希望所有数组都使用pubsite_id = 8,这是最佳方式。我可以使用foreach来做到这一点,但是当它变大时它似乎非常昂贵。

array(
  array( "startdate"    => "2014-02-01",
         "enddate"      => "2014-02-13",
         "pubsite_id"   => "1",
         "dfp_order_id" => "116260016",
         "amount"       => "-5000.00000",
         "amountperday" => -384.61538461538
  ),
  array( "startdate"    => "2014-02-01",
         "enddate"      => "2014-02-28",
         "pubsite_id"   => "8",
         "dfp_order_id" => "116260016",
         "amount"       => "-700.00000",
         "amountperday" => -25
  ),
  ...
)

2 个答案:

答案 0 :(得分:1)

Per @ deceze的建议,array_filter是一个很好的,干净的方式,并且会表现得很好:

$filtered_arr = array_filter( $arr, function( $item ) {
  return $item["pubsite_id"] == "8";
} )

答案 1 :(得分:-1)

foreach($array as $value) 
{
    foreach($value as $key => $val) 
    {
      if($val['pubsite_id'] == "8") 
     {
       /*your stuff */
     }

   }
}