0

So I have a variable $family.

$family = "mom dad sister brother";

I have to make an array $f from variable $family. Something like this

$f = array("mom", "dad", "sister", "brother");

Next thing that I have is $nice_family = "is nice";

Desired result : mom is nice, dad is nice, sister is nice, brother is nice.

Thanks in advance!

2
  • 3
    What have you tried? StackOverflow is not a request site. You can read more about how to ask a good question here: stackoverflow.com/help/how-to-ask Commented Jan 5, 2016 at 18:12
  • 3
    $f = explode(' ', $family); is a good start.... PHP Docs Commented Jan 5, 2016 at 18:12

3 Answers 3

5
$family = "mom dad sister brother";
$f = explode(' ', $family);
$nice = ' is nice';
echo $nice_family = implode($nice.', ' ,$f).$nice;
Sign up to request clarification or add additional context in comments.

Comments

1
$family = "mom dad sister brother";
$f = explode(' ', $family);
$result = array_map(function($val) { return $val . ' is nice';}, $f);

print_r($result);
Array
(
[0] => mom is nice
[1] => dad is nice
[2] => sister is nice
[3] => brother is nice
)

Comments

0

You can use explode to create the desired array:

$f = explode(' ', $family);

Then you could use implode to get the desired result string:

echo implode(' ' . $nice_family . ', ', $f) . ' ' . $nice_family . '.';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.