-1

I need to combine two PHP associative array values into one. Consider this code:

$period['duration'] = "35";
$period['timeunit'] = "Years";

print_r($period);

Outputs:

Array
(
    [duration] => 35
    [timeunit] => Years
)

But what I need to end up with is just:

Array
(
    [duration] => 35 Years
)

I've searched the documentation for this but can only find how to merge whole arrays or append new keys to the array etc.

This is because I have a HTML form that has a text field for the number, then a drop down for the unit (e.g years, months etc.)

I can't see a plain HTML way of combining two separate input fields before submitting via POST, otherwise I'd do that. I'm assuming PHP processing the post values afterwards is preferable to pre-processing with javascript.

  • @mickmackusa That question is completely different to mine. It's about concatenating two strings together, not appending to existing values in an array. – Jack Warren Jan 15 '20 at 01:21
  • `$period['duration']` is a string. `$period['timeunit']` is a string. – mickmackusa Jan 15 '20 at 01:22
  • `$period['duration'] .= ' ' . array_pop($period);` https://3v4l.org/5TPm8 – mickmackusa Jan 15 '20 at 01:29
  • 1
    Ah, I see. You can append using .= like with a regular string. I always thought of arrays as being different, but I guess each value is the same as a regular string. Many thanks for your help. – Jack Warren Jan 15 '20 at 01:37

2 Answers2

0

why you don't create a new array with the data that you want

$period['duration'] = "35";
$period['timeunit'] = "Years";
print_r($period);
$period2 = ['duration' => $period['duration'] . ' ' . $period['timeunit']];
//another way will be but if you have more elements not work like you expected
$period2['duration'] = implode(' ', $period); 
Emiliano
  • 698
  • 9
  • 30
  • Thanks @Emiliano ! That's one way round it, though I've decided to rethink my approach and perhaps have a third field that has the concatenated data instead. – Jack Warren Jan 15 '20 at 01:34
0

Two methods for you:

  1. Use dot to concatenate duration, space and timeunit, then remove timeunit from array:
$period['duration'] .= " " . $period['timeunit'];
unset($period['timeunit']);
  1. Use implode(), But this way only works if your array has only 2 elements:
$duration = implode(" ", $period);
$period = ['duration' => $duration];
Calos
  • 1,783
  • 19
  • 28
  • Thanks @Calos The first option is on the right lines, but it overwrites all other values in the array... (lets say I have 50 others unrelated to these two).. It should be: $period['duration'] => $duration; However, when I reload the values from the DB, it's obviously displaying wrong, so I actually need to totally rethink my approach after all! (I think I'll have a third array value with the concatenated data). Thanks for the help though! – Jack Warren Jan 15 '20 at 01:33
  • Okay, while this question is marked duplicated, I modified the first method based on your description, I hope to help you. – Calos Jan 15 '20 at 01:39
  • Amazing! Thanks, Calos. – Jack Warren Jan 17 '20 at 12:35