PHP 7数组列不适用于多维数组 [英] PHP 7 array columns not working for multidimensional array

查看:130
本文介绍了PHP 7数组列不适用于多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的数组结构:

Array
(
    [2016-09-01] => Array
        (
            [1] => Array
                (
                    [hours_type_id] => 1
                    [date] => 2016-09-01
                    [hours] => 8.00
                    [ts_weekly_id] => 53428
                )

            [2] => Array
                (
                    [hours_type_id] => 2
                    [date] => 2016-09-01
                    [hours] => 0.00
                    [ts_weekly_id] => 53428
                )

            [10] => Array
                (
                    [hours_type_id] => 10
                    [date] => 2016-09-01
                    [hours] => 0.00
                    [ts_weekly_id] => 53428
                )

        )

我正在尝试将所有小时数列放在另一个数组中.

I am trying to get all hours column in another array.

下面是代码:

$billcols   = array_column($billhours, "hours");

除了array_column之外,是否还有其他函数可用于获取数组中的列,该函数可用于多维数组,如上所示.

Is there any other function to get columns in array other than array_column which will work for multidimensional array like show above.

推荐答案

还有一个额外的维度,因此:

There is an extra dimension, so:

$billcols = array_column($billhours['2016-09-01'], "hours");

如果有多个日期,则循环并合并结果.我们在这里不使用 $ date ,仅是一个示例:

If there are multiple dates, just loop and merge the results. We don't use $date here, just an example:

$billcols = [];
foreach($billhours as $date => $array) {
    $billcols = array_merge($billcols, array_column($array, "hours"));
}

这篇关于PHP 7数组列不适用于多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆