0

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

我想收到所有在 [created] 中寻找特定日期 Ex(2012-07-25) 的密钥

并接收 array[0][0] 和 array[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
            )
    )

)
4

2 回答 2

2

像这样的东西应该适用于array_filter()

$target = '2012-07-25';
$matches = array_filter( $array[0], function( $el) use( $target) {
    return $el['created'] == $target;
);
于 2012-07-26T21:03:10.333 回答
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;
}

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

于 2012-07-26T21:09:31.390 回答