1

How to replace "City1" with random city on $substitutes

<?php 
$placeholders = 'City1 - City2 - City3 - City4';
$substitutes  = [
'City1' => ['Orlando,Dallas,Atlanta,Detroit'],
'City2' => ['Jakarta,Bandung,Surabaya'],
'City3' => ['Atlanta,Tampa,Miami'],
'City4' => ['Mandalay,Caloocan,Hai Phong,Quezon City'],
];
$replacements = [];
foreach($substitutes as $key => $choices) {
    $random_key = array_rand($choices);
    $replacements[$key] = $choices[$random_key];
}
$spun = str_replace(
    array_keys($replacements),
    array_values($replacements),
    $placeholders
);
echo $spun;
?>

And some output: Dallas - Jakarta - Miami - Mandalay

4 Answers 4

1

Your $substitutes array is not defined correctly. Try:

$substitutes = [
  'City1' => ['Orlando', 'Dallas', 'Atlanta', 'Detroit'],
  'City2' => ['Jakarta', 'Bandung', 'Surabaya'],
  'City3' => ['Atlanta', 'Tampa', 'Miami'],
  'City4' => ['Mandalay', 'Caloocan', 'Hai Phong', 'Quezon City']
]; 

Or if, for some reason, you can't change how $substitutes is defined, you can do the following to transform it into the proper form:

$substitutes = array_map(function ($cities) {
  return explode(',', $cities[0]);
}, $substitutes);
Sign up to request clarification or add additional context in comments.

Comments

0

Try This

 <?php 
    $placeholders = 'City1 - City2 - City3 - City4';
    $substitutes  = [
    'City1' => ['Orlando,Dallas,Atlanta,Detroit'],
    'City2' => ['Jakarta,Bandung,Surabaya'],
    'City3' => ['Atlanta,Tampa,Miami'],
    'City4' => ['Mandalay,Caloocan,Hai Phong,Quezon City'],
    ];
    $replacements = [];

    foreach($substitutes as $key => $choices) {
    $element = $choices[0];
    $elements=explode(',',$element);
    $randomElement = $elements[array_rand($elements, 1)];


        $placeholders= str_replace($key, $randomElement ,$placeholders);

    }
    echo $placeholders;
    ?>

It will produce the output as

Orlando - Bandung - Tampa - Hai Phong

1 Comment

in This case you no need to do any changes in your substitutes
0

You can do that this way as well.

$substitutes  = [
'City1' => ['Orlando','Dallas','Atlanta','Detroit'],
'City2' => ['Jakarta','Bandung','Surabaya'],
'City3' => ['Atlanta','Tampa','Miami'],
'City4' => ['Mandalay','Caloocan','Hai Phong','Quezon City'],
];

foreach($substitutes as $city=>$cities){

  $results[] = $substitutes[$city][array_rand($cities)];

}

echo '<pre>';
print_r($results);
echo '</pre>';

This will output:

Array
(
    [0] => Atlanta
    [1] => Bandung
    [2] => Miami
    [3] => Hai Phong
)

You can add this line and it will output it as a string if you want.

$string = implode(' - ', $results);
echo $string;

Like so:

Atlanta - Bandung - Miami - Hai Phong

Good luck!

Comments

0

How to make this spun result unique ?

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.