在php中连接n个数组的值 [英] Concatenate values of n arrays in php

查看:13
本文介绍了在php中连接n个数组的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有未知数量的数组,每个数组包含未知数量的单词.我想连接每个列表中的值,以便将单词的所有可能变体存储到最终数组中.

I have an unknown number of arrays, each containing an unknown number of words. I want to concatenate the values from each list so that all possible variations of the words are stored to a final array.

例如,如果数组 1 包含:

For example, if array 1 contains:

dog
cat

和数组 2 包含:

food
tooth

和数组 3 包含:

car
bike

我希望输出为:

dog food car
dog food bike
dog tooth car
dog tooth bike
cat food car
cat food bike
cat tooth car
cat tooth bike

可能有 3 个以上的列表,每个列表很可能有 2 个以上的词.

There could be more than 3 lists, and each list will most likely have more than 2 words.

我想在 PHP 中执行此操作.

I'd like to do this in PHP.

如果我知道列表的数量,我就知道该怎么做,尽管这可能不是最节省资源的方法.但是如果您知道数组的数量,嵌套的 foreach 循环就可以工作.如果你不这样做呢?如果假设有 100 个数组,每个数组包含 100 个单词,那么有哪些方法可以解决这个问题仍然有效.还是1000?

I know how to do it if I know the number of lists, though it's probably not the most resource efficient method. But nested foreach loops works if you know the number of arrays. What if you don't? And what are some methods to solve this problem that will still work if, let's say, there are 100 arrays of 100 words each. Or 1000?

谢谢!

推荐答案

您可以将所有单词数组放入一个数组中,并使用这样的递归函数:

You can put all word arrays into one array and use a recursive function like this:

function concat(array $array) {
    $current = array_shift($array);
    if(count($array) > 0) {
        $results = array();
        $temp = concat($array);
        foreach($current as $word) {
          foreach($temp as $value) {
            $results[] =  $word . ' ' . $value;
          }
        }
        return $results;           
    }
    else {
       return $current;
    }
}

$a = array(array('dog', 'cat'), array('food', 'tooth'), array('car', 'bike'));

print_r(concat($a));

哪个返回:

Array
(
    [0] => dog food car
    [1] => dog food bike
    [2] => dog tooth car
    [3] => dog tooth bike
    [4] => cat food car
    [5] => cat food bike
    [6] => cat tooth car
    [7] => cat tooth bike
)

但我猜这对于大数组来说表现不佳,因为输出数组会非常大.

But I guess this behaves badly for large arrays as the output array will be very big.

为了解决这个问题,您可以使用类似的方法直接输出组合:

To get around this, you can output the combinations directly, using a similar approach:

function concat(array $array, $concat = '') {
    $current = array_shift($array);

    $current_strings = array();

    foreach($current as $word) {
            $current_strings[] = $concat . ' ' . $word;
    }

    if(count($array) > 0) {
        foreach($current_strings as $string) {
            concat($array, $string);
        }       
    }
    else {
      foreach($current_strings as $string) {
          echo $string . PHP_EOL;
      }   
    }
}

concat(array(array('dog', 'cat'), array('food', 'tooth'), array('car', 'bike')));

给出:

dog food car
dog food bike
dog tooth car
dog tooth bike
cat food car
cat food bike
cat tooth car
cat tooth bike

使用这种方法也很容易获得子级联".只需插入 echo $string .PHP_EOL;concat($array, $string); 之前,输出为:

With this approach it is also easy to get the "sub-concatinations". Just insert echo $string . PHP_EOL; before concat($array, $string); and the output is:

 dog
 dog food
 dog food car
 dog food bike
 dog tooth
 dog tooth car
 dog tooth bike
 cat
 cat food
 cat food car
 cat food bike
 cat tooth
 cat tooth car
 cat tooth bike

这篇关于在php中连接n个数组的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆