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!
PHP | join() Function The join() function is built-in function in PHP and is used to join an array of elements which are separated by a string. Parameter: The join() function accepts two parameter out of which one is optional and one is mandatory. $array : The array whose value is to be joined to form a string.
The array_combine() function creates an array by using the elements from one "keys" array and one "values" array. Note: Both arrays must have equal number of elements!
array_merge(array ...$arrays ): array. Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. If the input arrays have the same string keys, then the later value for that key will overwrite the previous one.
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));
Which returns:
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')));
Which gives:
dog food car
dog food bike
dog tooth car
dog tooth bike
cat food car
cat food bike
cat tooth car
cat tooth bike
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With