0

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.

3
  • 1
    $billcols = array_column($billhours['2016-09-01'], "hours"); or loop. Commented Feb 27, 2017 at 19:56
  • that we can do easily, i mean what if there another date index in same array Commented Feb 27, 2017 at 19:58
  • This array is a range for date span say from one date to another date. Commented Feb 27, 2017 at 19:59

1 Answer 1

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"));
}
Sign up to request clarification or add additional context in comments.

1 Comment

$billcols = []; and then $billcols += ? :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.