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
            )
    )
)

类似的东西应该适用于array_filter():

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

还没有测试过,但它应该可以工作:

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

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

最新更新