在php中连接n个数组的值
我有未知数量的数组,每个数组包含未知数量的单词。我想连接每个列表中的值,以便单词的所有可能变体都存储到最终数组中。
例如,如果数组 1 包含:
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 个单词。
我想用 PHP 来做这个。
如果我知道列表的数量,我就知道该怎么做,尽管这可能不是最节省资源的方法。但如果您知道数组的数量,嵌套的 foreach 循环就可以工作。如果你不这样做怎么办?如果有 100 个数组,每个数组有 100 个单词,那么有哪些方法可以解决这个问题仍然有效。还是1000?
谢谢!
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.
For example, if array 1 contains:
dog
cat
and array 2 contains:
food
tooth
and array 3 contains:
car
bike
I'd like the output to be:
dog food car
dog food bike
dog tooth car
dog tooth bike
cat food car
cat food bike
cat tooth car
cat tooth bike
There could be more than 3 lists, and each list will most likely have more than 2 words.
I'd like to do this in PHP.
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?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将所有单词数组放入一个数组中,并使用一个递归函数,如下所示:
返回:
但我想这对于大型数组来说效果很差,因为输出数组将非常大。
为了解决这个问题,您可以使用类似的方法直接输出组合:
给出:
通过这种方法,也很容易获得“子连接”。只需插入
echo $string 。 PHP_EOL;
在concat($array, $string);
之前,输出为:You can put all word arrays into one array and use a recursive function like this:
Which returns:
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:
Which gives:
With this approach it is also easy to get the "sub-concatinations". Just insert
echo $string . PHP_EOL;
beforeconcat($array, $string);
and the output is:您可以枚举结果集的元素,即对于 0...(元素数量)-1 之间的每个整数,您可以知道要返回哪个元素(即存在自然顺序)。对于给定的示例:
您所需要的只是一个(整数)索引 n 和一个将索引“转换”为(自然排序)集合的第 n 个元素的函数。由于您只需要一个整数来存储当前状态,因此当您有许多/大型数组时,内存消耗不会“爆炸”。正如克里斯在评论中所说,您可以用速度(使用较小的集合时)来换取低内存消耗。 (虽然我认为 - php 的实现方式 - 这也是一个合理的快速解决方案。)
您可以使用它来实现例如 迭代器,一个 SeekableIterator 甚至可能是一个 ArrayAccess (从而与递归解决方案相比反转了控制,几乎就像
在Python或Ruby中yield
)打印
(序列似乎没问题;-))
You can enumerate the elements of the result set, i.e. for each integer between 0....(number of elements)-1 you can tell which element to return (i.e. there is a natural order). For the given example:
All you need is a (integer) index n and a function that "translates" the index to the nth element of the (natural ordered) set. Since you only need an integer to store the current state the memory consumption doesn't "explode" when you have many/large arrays. As chris said in his comment, you trade speed (when using smaller sets) for low memory consumption. (Though I think -the way php is implemented- this is also a reasonable fast solution.)
You can use this to implement e.g. an Iterator, a SeekableIterator or maybe even an ArrayAccess (and thereby inverting the control compared to the recursive solutions, almost like a
yield
in python or ruby)prints
( the sequence seems to be ok ;-) )
我还没有在巨大的单词列表上测试过这个,但它在中等大小的列表上速度相当快,并且不使用递归,我认为(如果我错了,请纠正我)可能会导致内存限制问题:
I haven't tested this on huge word lists, but it's pretty fast on moderately sized lists and doesn't use recursion, which I think (please correct me if I'm wrong) is probably causing the memory limit problems:
我的看法
My take