Php多阵列搜索

时间:2012-07-26 20:53:05

标签: php arrays multidimensional-array

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

我希望在[创建]

中收到所有寻找特定日期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
            )
    )

)

2 个答案:

答案 0 :(得分:2)

这样的事情应该适用于array_filter()

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

答案 1 :(得分:0)

Haven没有对此进行测试,但它应该有效:

$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;
}

就个人而言,我会完全放弃你的数据格式,并使用更适合这项任务的对象模型。