简体   繁体   English

如何创建递归函数以创建最终数组?

[英]How can I create recursive function to create final array?

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. 另一个与“ city”和“ make”答案相关联的数组。

$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]). 我正在寻找一种解决方案,当答案数组中有三个项目时(例如:[make] [model] [city]),该解决方案也应适用。 How can I achieve this? 我该如何实现? Any ideas appreciated! 任何想法表示赞赏!

Thanks 谢谢

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;
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM