0

This problem just slapped my face that I wasn't a great developer as much as I thought I was :) I cannot figure it out how to solve it. I'm assuming there should be a recursive function to do this but I'm open to any ideas.

Basically I have an array which is generated from user input.

$answers = [
  'city' => ["Los Angeles", "San Diego", "Hollywood"],  
  'make' => ["Ferrari", "Bugatti", "Lamborghini"],
];

And another array that's associated with "city" and "make" answers.

$template = [
    ["Campaign L", "Best [make] in [city]", "Active"],
    ["Campaign O", "Cheap [make] in [city]", "Pause"],
    ["Campaign V", "Top [make] in [city]", "Active"],
    ["Campaign E", "[make] [city]", "Pause"],
    ["Campaign C", "Buy Now [make] in [city]", "Active"],
    ["Campaign A", "Sale [make] [city]", "Active"],
    ["Campaign R", "Fast [make] in [city]", "Active"],
    ["Campaign S", "Red [make] [city]", "Active"],
];

What I'm trying to do is to create a final output where I replace each answers in the template and generate a main output. I have a function to replace the tags so I'm not worried about that part however I cannot figure out how to loop through each combination.

There should be combination of each city and make so my final output should be like this:

$finalOutput = [
    ["Campaign L", "Best Ferarri in Los Angeles", "Active"],
    ["Campaign O", "Cheap Ferarri in Los Angeles", "Pause"],
    ["Campaign V", "Top Ferarri in Los Angeles", "Active"],
    ["Campaign E", "Ferarri Los Angeles", "Pause"],
    ["Campaign C", "Buy Now Ferarri in Los Angeles", "Active"],
    ["Campaign A", "Sale Ferarri Los Angeles", "Active"],
    ["Campaign R", "Fast Ferarri in Los Angeles", "Active"],
    ["Campaign S", "Red Ferarri Los Angeles", "Active"],
    ["Campaign L", "Best Bugatti in Los Angeles", "Active"],
    ["Campaign O", "Cheap Bugatti in Los Angeles", "Pause"],
    ["Campaign V", "Top Bugatti in Los Angeles", "Active"],
    ["Campaign E", "Bugatti Los Angeles", "Pause"],
    ["Campaign C", "Buy Now Bugatti in Los Angeles", "Active"],
    ["Campaign A", "Sale Bugatti Los Angeles", "Active"],
    ["Campaign R", "Fast Bugatti in Los Angeles", "Active"],
    ["Campaign S", "Red Bugatti Los Angeles", "Active"],
    ...
    ...
    ...
    ...
   and goes on with other combinations...
];

I'm looking for a solution where it should also work when there are three items in the answer array (ex: [make] [model] [city]). How can I achieve this? Any ideas appreciated!

Thanks

3
  • And what have you tried to do so far Commented Feb 11, 2016 at 2:31
  • Hmm nothing got anywhere close so far. I have tried creating foreach loops since I can get a count of the answers array keys but the problem is that I have to provide the updated template back to the function so it can put the next answer. But I couldn't figure out how to provide that. Also I'm not sure how to loop more when there are three items. Commented Feb 11, 2016 at 2:35
  • Show what you have done, at least it gives us a start point Commented Feb 11, 2016 at 2:38

1 Answer 1

1

Here's a corrected version. A little sloppy, was just going for the quickest solution, but it should give you a place to start.

foreach($template as $fields => $rows) {

    $fields = explode(' ', $fields);

    foreach($fields as $i => $field) {
        $replacements[$i] = $answers[$field];
    }

    $first_answers = array_shift($replacements);
    $first_field   = array_shift($fields);

    for($i = 0; $i < count($first_answers); $i++) {
        foreach($replacements as $j => $values) {
            foreach($values as $replacement) {
                foreach($rows as $row) {
                    $new_row = [];
                    foreach($row as $col => $text) {
                        $text = str_replace('['.$first_field.']', $first_answers[$i], $text);
                        $text = str_replace('['.$fields[$j].']', $replacement, $text);
                        $new_row[] = $text;
                    }
                    $finalOutput[] = $new_row;
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

You're missing brackets in the $template array (should be "city make" => [), but other than that this is tested working.
Thank you for the catch and thank you for the answer. I just corrected the question. I'm going to give it a try.
hmm thanks again for the answer but it just loops once.
Did you change your array structure? It works fine for me and on codepad: working example
I just updated however when you look at the codepad you only see 8 results. It should be 81 combinations.
|

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.