0

This one should be easy, as it is clearly just a question my getting the syntax wrong. I just need to merge two arrays while adding some extra characters to each array value...

This was my attempt:

$time = array_merge($hour, ':', $minute, ':00');

...which doesn't work. The output should be e.g.: 20:00:00 22:00:00 24:00:00 etc.

Paul Clift
  • 169
  • 8
  • 1
    Can you show what `$hour` and `$minute` may contain and how they should be combined. `array_merge()` isn't suitable for this. – Nigel Ren Nov 24 '18 at 09:40
  • show the values of `$hour` a and `$minute` also. share some more code. – Madhur Sharma Nov 24 '18 at 09:40
  • Ahh, damn! I thought that I was close. – Paul Clift Nov 24 '18 at 09:40
  • Those two arrays are defined by a form with a variable number of inputs on a previous page. So: $hour = ($_POST['hour']); // $minute = ($_POST['minute']); both will be arrays containing one or more INTs each. – Paul Clift Nov 24 '18 at 09:43
  • if `$hour` and `$minute` are array both of them, so how is the relation between them? I mean how do you know which hour is related to which minute? – FatemehNB Nov 24 '18 at 09:45
  • $hour [1] should go with minute[1], $hour [2] should go with minute[2], etc. both arrays will always be of the same length. – Paul Clift Nov 24 '18 at 09:49

2 Answers2

2

array_merge will merge n number of array type only and not string. You may need to first add the extra characters as item in each array and then do the array_merge.

for e.g.

array_push($hour, ":");
array_push($minute, ":00");
$time = array_merge($hour, $minute);

However, the output suggests that you are looking for array_map and not array_merge.

$time = array_map(function($a, $b) { return $a . ':' . $b . ':00'; }, $hour, $minute));
Sonal Borkar
  • 531
  • 1
  • 6
  • 12
1

There are two possibilities I can see for combining both arrays.

Option 1 is to combine the corresponding items from each array...

$hour = [20,22,24];
$minute = [0,10,20];

$out = [];
foreach ( $hour as $key => $hr )    {
    $out[] = $hr. ':'. str_pad( $minute[$key], 2, '0', STR_PAD_LEFT). ':00';
}
print_r($out);

which gives...

Array
(
    [0] => 20:00:00
    [1] => 22:10:00
    [2] => 24:20:00
)

Option 2 is to combine all possibilities...

$out = [];
foreach ( $hour as $hr )    {
    foreach ( $minute as $mnt ){
        $out[] = $hr. ':'. str_pad( $mnt, 2, '0', STR_PAD_LEFT). ':00';
    }
}
print_r($out);

which gives...

Array
(
    [0] => 20:00:00
    [1] => 20:10:00
    [2] => 20:20:00
    [3] => 22:00:00
    [4] => 22:10:00
    [5] => 22:20:00
    [6] => 24:00:00
    [7] => 24:10:00
    [8] => 24:20:00
)

As they are both integers, I use str_pad() on the minutes to ensure that it is always 2 digits. You could do the same with the hours if you needed to.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Ah, so there is no way of automatically merging two arrays when you don't know how many values they will contain? To be clear, in my example, both arrays will always contain the same number of values.... – Paul Clift Nov 24 '18 at 09:47
  • The difference in that just merging an array - as in combining all the values into one array could use `array_merge()`, but you are combining the values of the two arrays into a specific format. This extra manipulation is why (AFAIK) there isn't a magic method to add them together. – Nigel Ren Nov 24 '18 at 09:50
  • I see the problem. Your first example does give me the output I need though! – Paul Clift Nov 24 '18 at 09:55