2012-07-26 92 views
0

如何搜索多维数组中的值。Php多数组搜索

我想接收在[创建]

寻找一个特定的日期EX(2012-07-25)中的所有项和接收阵列[0] [0]和数组[0] [1]

Array 
(
[0] => Array 
    (
     [0] => Array 
      (
       [id_store] => 3 
       [id_product] => 11 
       [monitored] => 0 
       [created] => 2012-07-25 
      ) 

     [1] => Array 
      (
       [id_store] => 3 
       [id_product] => 12 
       [monitored] => 0 
       [created] => 2012-07-25 
      ) 

     [2] => Array 
      (
       [id_store] => 4 
       [id_product] => 11 
       [monitored] => 0 
       [created] => 2012-07-26 
      ) 
    ) 

) 
+0

array_search()的用户注释中有几个例子:http://nz.php.net/manual/en/function.array-search.php – 2012-07-26 20:55:55

回答

2

这样的事情应该用array_filter()工作:

$target = '2012-07-25'; 
$matches = array_filter($array[0], function($el) use($target) { 
    return $el['created'] == $target; 
); 
0

没有测试过这一点,但它应该工作:

$results = search_date($my_array, $my_date); 

`

function search_date($array, $date, &$result=array()) 
{ 
    foreach($array as $key=>$value) 
    { 
     if($key == 'created' && $value == $date) 
     { 
      $result[]=$array; 
      continue; 
     } 
     if(is_array($item)) 
     { 
      search_date($item, $date, $result); 
     } 
    } 
    return $result; 
} 

就个人而言,我将完全抛弃你的数据格式和使用对象模型更适合的任务。