Question

I have a function that merges a bunch of bookings from different groups. I want to make it easier to set the group. Heres the function at the moment:

function groups() {
    $b1 = bookings(1);
    $b2 = bookings(2);

    $merge = array_merge($b1, $b2);
    return $merge;
}

I would like to make it look something like this:

function groups() {
    $merge = bookings(1), bookings(2);
    return $merge;
}

The reason is I would like to only have to edit one place if I would like to add a group. Now you have to add $b3 = bookings(3); on one line and $b3 in the array_merge.

Is this possible?

Was it helpful?

Solution

if and only if the arrays have different keys, you can use the + operator to union the two arrays. If the arrays contain the same key (eg. default indexes), only the first one will be kept and the rest will be omitted.

eg:

$arr1 = array("color1" => "red", "color2" => "blue");
$arr2 = array("color1" => "black", "color3" => "green");
$arr3 = $arr1 + $arr2; //result is array("color1" => "red", "color2" => "blue", "color3" => "green");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top