在PHPñ阵列的串连值 [英] Concatenate values of n arrays in php

查看:200
本文介绍了在PHPñ阵列的串连值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数目不详的阵列,每个都包含一个未知的字数。我想,这样的话所有可能的变化都存储到最终阵列从每个列表串连的值。

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列表,每个列表将极有可能超过200字。

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

我想这样做在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

通过这种方法也容易得到分concatinations。只需插入回声$字符串。 PHP_EOL; CONCAT($数组$字符串); ,输出是:

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ñ阵列的串连值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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