PHP 7 array columns not working for multidimensional array

倖福魔咒の 提交于 2021-02-04 21:33:54

问题


Below is my array structure:

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.

Below is the code:

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

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


回答1:


There is an extra dimension, so:

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

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"));
}


来源:https://stackoverflow.com/questions/42494599/php-7-array-columns-not-working-for-multidimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!