-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.

4
  • @mickmackusa That question is completely different to mine. It's about concatenating two strings together, not appending to existing values in an array. Commented Jan 15, 2020 at 1:21
  • $period['duration'] is a string. $period['timeunit'] is a string. Commented Jan 15, 2020 at 1:22
  • $period['duration'] .= ' ' . array_pop($period); 3v4l.org/5TPm8 Commented Jan 15, 2020 at 1: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. Commented Jan 15, 2020 at 1:37

2 Answers 2

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

1 Comment

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.
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];

3 Comments

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!
Okay, while this question is marked duplicated, I modified the first method based on your description, I hope to help you.
Amazing! Thanks, Calos.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.