Php 如何按键从多维数组中获取父键

Php 如何按键从多维数组中获取父键,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我正在尝试获取其中一个“main”数组('111','222'),其中“expire”属性已从当前时间戳中过期 我的数组的结构如下所示: $stored=array( '111'=>数组( 0=>数组( 'type'=>'first', “到期”=>日期('Y-m-d H:i:s',标准时间('2019-01-01 08:00:00'), ), 1=>数组( 'type'=>'second', “到期”=>日期('Y-m-d H:i:s',标准时间('2019-01-01 09:00:00'),

我正在尝试获取其中一个“main”数组('111','222'),其中“expire”属性已从当前时间戳中过期

我的数组的结构如下所示:

$stored=array(
'111'=>数组(
0=>数组(
'type'=>'first',
“到期”=>日期('Y-m-d H:i:s',标准时间('2019-01-01 08:00:00'),
),
1=>数组(
'type'=>'second',
“到期”=>日期('Y-m-d H:i:s',标准时间('2019-01-01 09:00:00'),
),
),
'222'=>数组(
0=>数组(
'type'=>'first',
“到期”=>日期('Y-m-d H:i:s',标准时间('2019-01-02 12:00:00'),
),
1=>数组(
'type'=>'second',
“到期”=>日期('Y-m-d H:i:s',标准时间('2019-01-02 13:00:00'),
),
2=>数组(
'类型'=>'第三',
“到期”=>日期('Y-m-d H:i:s',标准时间('2019-01-02 14:00:00'),
),
),
);
我做了一些研究,发现了
array\u column
,我觉得这是解决方案的一部分,但我不知道如何将其应用到我的案例中,因为我尝试过

$list=array_列($stored,'expire');
打印(列表);

它给出了一个空数组,因此这意味着
array\u column
在多维数组上不起作用。

您可以循环遍历每个项目集合,然后使用
array\u filter
确定是否有任何项目过期。例如:

$expiredKeys=[];
foreach($存储为$key=>$items){
$hasExpired=计数(数组\过滤器($items,函数($item){
返回strotime($item['expire']) 0;
如果($已过期){
$expiredKeys[]=$key;
}
}
var_dump($expiredKeys);
产出:

array(2) {
  [0] =>
  int(111)
  [1] =>
  int(222)
}

当内部循环检查过期条目时,两个嵌套的
foreach
循环是最有效的(IMHO)。一旦找到一个,它将标记整个关键点,然后移动到下一个关键点。这样就不必处理所有条目,然后检查是否还有剩余条目

$expired = [];
foreach ( $stored as $key => $main )    {
    foreach ( $main as $sub )   {
        if ( strtotime($sub['expire']) < time() )   {
            $expired[] = $key;
            break;
        }
    }
}
print_r($expired);

一旦找到过期的项目,它将立即停止这两个循环。

使用您的代码

// an array to hold all the expired dates
$expired = [];

$stored = array(
        '111' => array(
            0 => array(
                'type' => 'first',
                'expire' => date('Y-m-d H:i:s', strtotime('2019-08-01 08:00:00')),
            ),
            1 => array(
                'type' => 'second',
                'expire' => date('Y-m-d H:i:s', strtotime('2019-01-01 09:00:00')),
            ),
        ),
        '222' => array(
            0 => array(
                'type' => 'first',
                'expire' => date('Y-m-d H:i:s', strtotime('2019-01-02 12:00:00')),
            ),
            1 => array(
                'type' => 'second',
                'expire' => date('Y-m-d H:i:s', strtotime('2019-01-02 13:00:00')),
            ),
            2 => array(
                'type' => 'third',
                'expire' => date('Y-m-d H:i:s', strtotime('2019-01-02 14:00:00')),
            ),
        ),
);

// Loop through the first/parent array
foreach($stored as $key => $val){
    // Get the expire index only and store into variable for use in the inner loop
    $expire_dates = array_column($val, 'expire');
    // Loop through the inner arrays
    foreach($val as $value){
        // Convert the expire date from 'Y-m-d H:i:s' to unix timestamp, it is easier and better for date comparison
        $expire = strtotime($value['expire']);
        if($expire < time() ){
            $expired[$key][] = $value['expire'];
        }
    }

}

// Now print all the expired array data
echo '<pre>';
print_r($expired);

您可以将
array\u filter()
与一个回调一起使用,该回调将删除
过期
值在过去的任何值。您只需要第一个这样的键,还是所有键?哪个子元素具有这样的expires值并不重要?//在使用“奇特”数组函数之前,我建议您尝试使用简单的foreach循环来解决这个问题。基本逻辑通常很容易表达,而使用数组函数时,您需要非常好地理解它们的实际功能……最好将
time()
存储在变量中?最好将
time()
存储在变量中?@vivek_23最好将数组中的项作为时间戳存储。与过滤所有项目相比,这是一个微小的变化。同意,但不确定OP为什么要将其保持为日期格式。起初,我想避免使用循环,因为我认为它会带来更多的“负担”而不是“利润”(效率方面)。但是现在我认为差别非常小,所以我将使用这个方法。谢谢大家@atymic的答案也类似于这个,但是这个对我来说更容易阅读,所以我选择这个作为答案。
break 2;
// an array to hold all the expired dates
$expired = [];

$stored = array(
        '111' => array(
            0 => array(
                'type' => 'first',
                'expire' => date('Y-m-d H:i:s', strtotime('2019-08-01 08:00:00')),
            ),
            1 => array(
                'type' => 'second',
                'expire' => date('Y-m-d H:i:s', strtotime('2019-01-01 09:00:00')),
            ),
        ),
        '222' => array(
            0 => array(
                'type' => 'first',
                'expire' => date('Y-m-d H:i:s', strtotime('2019-01-02 12:00:00')),
            ),
            1 => array(
                'type' => 'second',
                'expire' => date('Y-m-d H:i:s', strtotime('2019-01-02 13:00:00')),
            ),
            2 => array(
                'type' => 'third',
                'expire' => date('Y-m-d H:i:s', strtotime('2019-01-02 14:00:00')),
            ),
        ),
);

// Loop through the first/parent array
foreach($stored as $key => $val){
    // Get the expire index only and store into variable for use in the inner loop
    $expire_dates = array_column($val, 'expire');
    // Loop through the inner arrays
    foreach($val as $value){
        // Convert the expire date from 'Y-m-d H:i:s' to unix timestamp, it is easier and better for date comparison
        $expire = strtotime($value['expire']);
        if($expire < time() ){
            $expired[$key][] = $value['expire'];
        }
    }

}

// Now print all the expired array data
echo '<pre>';
print_r($expired);
Array
(
    [111] => Array
        (
            [0] => 2019-01-01 09:00:00
        )

    [222] => Array
        (
            [0] => 2019-01-02 12:00:00
            [1] => 2019-01-02 13:00:00
            [2] => 2019-01-02 14:00:00
        )

)