Viewed   196 times

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.

 Answers

5

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"));
}
Sunday, November 20, 2022
 
iglvzx
 
5

It is cleary written here that last parameter is_dst of mktime has been removed in PHP 7, You have to give 6 parameters instead of 7.

Try this code snippet here 7.0.8

<?php

ini_set('display_errors', 1);
$month_options = "";
for ($i = 1; $i <= 12; $i++)
{
    $month_num = str_pad($i, 2, 0, STR_PAD_LEFT);
    $month_name = date('F', mktime(0, 0, 0, $i + 1, 0, 0));
    $selected = "";
    $month_options .= $month_name . "<br/>";
}
echo $month_options;
Tuesday, December 20, 2022
 
1

You could do this using array_map() and array_sum():

$output = array_map(function($a) { 
    return is_array($a) ? array_sum($a) : $a; 
}, $myArray);

Here's a demo

Friday, August 12, 2022
 
nitram
 
3

Try this solution. You can add any count of arrays. But keep names as $arr1-$maxArraysCount

$arr1 = array(
    "Friday" => array(
        "Breakfast" => 32,
        "Lunch" => 45
    ),
    "Sunday" => array(
        "Lunch" => 12
    )
);

$arr2 = array(
    "Sunday" => array(
        "Breakfast" => 7,
        "Lunch" => 3
    ),
    "Monday" => array(
        "Breakfast" => 12
    )
);

$arr3 = array(
    "Monday" => array(
        "Breakfast" => 31
    )
);


$maxArraysCount = 8;
$return = array();
for($i = 1; $i < $maxArraysCount; $i++){
    $arr = 'arr' . $i;
    if(isset($$arr) && is_array($$arr)){
        foreach ($$arr as $day => $value) {
            foreach ($value as $eat => $count) {
                if(!isset($return[$day][$eat])) $return[$day][$eat] = 0;
                $return[$day][$eat] = $count + $return[$day][$eat];
            }
        }
    }
}

echo "<pre>";print_r($return);

Here is output:

Array
(
    [Friday] => Array
        (
            [Breakfast] => 32
            [Lunch] => 45
        )

    [Sunday] => Array
        (
            [Lunch] => 15
            [Breakfast] => 7
        )

    [Monday] => Array
        (
            [Breakfast] => 43
        )

)
Saturday, September 24, 2022
3

This is what I would do:

<?php
$promo = Array (
   Array ('insertDate' => '2014-11-10 11:23:08', 'keyword' => 'promo', 'mediaClass' => 'image', 'mediaURL' => 'http://image2.jpg', 'promoURL' => 'http://www.google.com'),
   Array ('insertDate' => '2014-11-10 11:23:38', 'keyword' => 'promo', 'mediaClass' => 'image', 'mediaURL' => 'http://image5.jpg', 'promoURL' => 'http://www.google.com')
 );

$media = Array (
  Array ('insertDate' => '2014-11-10 11:22:58', 'keyword' => 'standard', 'mediaClass' => 'image', 'mediaURL' => 'http://image1.jpg', 'promoURL' => '', ),
  Array ('insertDate' => '2014-11-10 11:23:18', 'keyword' => 'standard', 'mediaClass' => 'image', 'mediaURL' => 'http://image3.jpg', 'promoURL' => '', ),
  Array ('insertDate' => '2014-11-10 11:23:28', 'keyword' => 'standard', 'mediaClass' => 'image', 'mediaURL' => 'http://image4.jpg', 'promoURL' => '', ),
  Array ('insertDate' => '2014-11-10 11:23:48', 'keyword' => 'standard', 'mediaClass' => 'image', 'mediaURL' => 'http://image6.jpg', 'promoURL' => '', )
);

//sort the promo in random order. This ensures they go into media in random order
shuffle($promo);

//get random keys to insert the promo image before
$randKeys = array_rand($media, count($promo));
//sort by the random key value in reverse. By inserting in reverse order,
//we won't have an issue with needing to increment keys to prevent two promo
//images ending up next to each other. Also, this is why we shuffle the promo
//array above, so the promo images go in random order because this is no longer random.
rsort($randKeys);

//loop over the random keys and insert the next promo image before each key
foreach($randKeys as $key){
    //get the first promo image and remove it
    $promoImage = array_shift($promo);
    //splice the promo image into the media array
    array_splice($media, $key, 0, array($promoImage));
}

//display
print_r($media);

Only thing to note is that both my media and promo arrays are numeric index based starting at zero and contiguous which is default for an array and not like the arrays you posted where they 1 and 4 keys are removed from the media array.

Tuesday, October 4, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :