多维数组过滤PHP

时间:2015-03-05 15:33:54

标签: php arrays multidimensional-array

这是我的阵列:

Array ( [0] => Array ( [license_id] => 172 [valid_from] => 2014-10-13 14:39:32 [valid_till] => 2020-10-22 00:00:00 [us_user_user_id] => 810 [us_group_group_id] => [li_voucher_voucher_id] => 90128 [pr_product_product_ids] => 91,92,93 [li_license_setting_license_setting_id] => 55 [li_voucher_setting_voucher_setting_id] => 222 [product_data] => {"answer_layer":true} ) [1] => Array ( [license_id] => 173 [valid_from] => 2014-10-13 14:39:48 [valid_till] => 2020-10-21 14:39:48 [us_user_user_id] => 810 [us_group_group_id] => [li_voucher_voucher_id] => 90129 [pr_product_product_ids] => 94 [li_license_setting_license_setting_id] => 73 [li_voucher_setting_voucher_setting_id] => 223 [product_data] => {"answer_layer":true} ) [2] => Array ( [license_id] => 371 [valid_from] => 2015-01-07 12:05:36 [valid_till] => 2021-01-15 12:05:36 [us_user_user_id] => 810 [us_group_group_id] => [li_voucher_voucher_id] => 89008 [pr_product_product_ids] => 173 [li_license_setting_license_setting_id] => 56 [li_voucher_setting_voucher_setting_id] => 160 [product_data] => {"answer_layer":true} ) [3] => Array ( [license_id] => 441 [valid_from] => 2015-03-04 16:10:18 [valid_till] => 2016-03-03 16:10:18 [us_user_user_id] => 810 [us_group_group_id] => [li_voucher_voucher_id] => 124457 [pr_product_product_ids] => 243 [li_license_setting_license_setting_id] => 201 [li_voucher_setting_voucher_setting_id] => 315 [product_data] => ) )

我想过滤[valid_till]的值,其中[license_id]的值为441.

我已经使用license_id 441使用下一个代码过滤了特定许可证:

$filtered = array_filter($userlicenses, function($v) { return $v['license_id'] == '441'; });

但是这给了我完整的数组。我只想要[valid_till],其中[license_id] = 441。

2 个答案:

答案 0 :(得分:0)

"迭代数组中的每个值,将它们传递给回调函数。如果回调函数返回true,则将数组中的当前值返回到结果数组中。数组键被保留。" function.array-filter

这意味着您的变量$filtered再次/仍然是一个数组。 如果它只是一维(即你总是只有一个结果),你可以简单地访问以这种方式返回的第一个元素的键valid_till

<?php

$userlicenses = array( 
    0 => array ( 
        'license_id' => 172,
        'valid_from' => '2014-10-13 14:39:32',
        'valid_till' => '2020-10-22 00:00:00'
    ),
    1 => array ( 
        'license_id' => 173,
        'valid_from' => '2014-10-13 14:39:48',
        'valid_till' => '2020-10-21 14:39:48'
    ),
    2 => array ( 
        'license_id' => 371,
        'valid_from' => '2015-01-07 12:05:36',
        'valid_till' => '2021-01-15 12:05:36'
    ),
    3 => array ( 
        'license_id' => 441,
        'valid_from' => '2015-03-04 16:10:18',
        'valid_till' => '2016-03-03 16:10:18'
    )
);

$filtered = array_filter($userlicenses, function($v) { return $v['license_id'] == '441'; });
var_dump( $filtered );
if( is_array( $filtered ) )
{
   $first_el = reset( $filtered );
   $valid_till = $first_el['valid_till'];
   var_dump( $valid_till );
}

?>

答案 1 :(得分:0)

array_reduce是一种简单的方法来返回值:

$result = array_reduce($userlicenses,

                       function($c, $v) {
                            return $v['license_id'] == '441' ? $v['valid_till'] : false;
                       }
);

array_column方法:

$result = array_column($userlicenses, 'valid_from', 'license_id')[441];