如何从未知数量的数组 PHP 构建数组 [string]
How to build an array[string] from an unknown amount of arrays PHP
我怀疑这样做是否正确。
会发生什么?
您为方法提供任意数量的数组...
[
'jupiter',
'saturn',
]
[
'is',
'was',
]
[
'old',
'big',
'loved',
]
并创建...
[
'jupiter is big',
'jupiter is loved',
'jupiter is old',
'jupiter was big',
'jupiter was loved',
'jupiter was old',
'saturn is big',
'saturn is loved',
'saturn is old',
'saturn was big',
'saturn was loved',
'saturn was old',
]
你应该怎么做?
Whosebug 喜欢尝试,所以给你
我的方法目前有两个循环函数(带有嵌套的 for 循环)和十几个其他的 for 循环,这不是您想象的实现此目的的最快方法。我确实首先尝试使用聪明的数学,但开始让自己感到困惑。如果您有更好的解决方案,请您也解释一下!
感谢您的宝贵时间。
这是我的工作代码:
<?php
function generateList(...$arrays)
{
// Use first array to generate a starting point
$result = [...array_map(function($item){return [$item => []];}, $arrays[0])];
// Adds a new keyed array to the end of every existing empty array
function addToEndChild($object, $string){
if (is_array($object) && count($object) == 0){
return [$string => []];
}
foreach ($object as $index => $item){
$object[$index] = addToEndChild($item, $string);
}
return $object;
};
// Flattens array back down using the keys as values
function flatten($array, $prefix = '', $depth = 0) {
$result = [];
foreach($array as $key => $value) {
if (is_int($key) == false){
$result[] = $key;
}
$flat = flatten($value, $prefix . $key . '.', $depth + 1);
$result = array_merge($result, $depth == 0 ? [$flat] : $flat);
}
return $result;
}
// Loop through remaining arrays and build up nested array
foreach (array_slice($arrays, 1) as $array){
$layer = [];
foreach ($array as $item){
$layer[] = addToEndChild($result, $item);
}
$result = array_merge(...$layer);
}
// Flatten down
$finalResult = flatten($result);
// Convert layers into strings
foreach ($finalResult as $index => $item){
$finalResult[$index] = implode(' ', $item);
}
// Sort alphabetically
asort($finalResult);
return $finalResult;
}
$a = [
'jupiter',
'saturn',
];
$b = [
'is',
'was',
];
$c = [
'old',
'big',
'loved',
];
var_dump(generateList($a, $b, $c));
这是我的代码,只有一个递归函数。而且它以一个矩阵作为参数而不是N个数组。
function generateList($data){
if (count($data) == 0){
return null;
}
$current = $data[0];
$left = array_slice($data, 1);
$permutations = generateList($left);
$r = [];
foreach($current as $item){
if ($permutations){
foreach ($permutations as $rest) {
$r[] = $item . ' ' . $rest;
}
}
else {
$r[] = $item;
}
}
return $r;
}
$data = [
['jupiter', 'saturn'],
['is', 'was'],
['old', 'big', 'loved']
];
var_dump(generateList($data));
我怀疑这样做是否正确。
会发生什么?
您为方法提供任意数量的数组...
[
'jupiter',
'saturn',
]
[
'is',
'was',
]
[
'old',
'big',
'loved',
]
并创建...
[
'jupiter is big',
'jupiter is loved',
'jupiter is old',
'jupiter was big',
'jupiter was loved',
'jupiter was old',
'saturn is big',
'saturn is loved',
'saturn is old',
'saturn was big',
'saturn was loved',
'saturn was old',
]
你应该怎么做?
Whosebug 喜欢尝试,所以给你
我的方法目前有两个循环函数(带有嵌套的 for 循环)和十几个其他的 for 循环,这不是您想象的实现此目的的最快方法。我确实首先尝试使用聪明的数学,但开始让自己感到困惑。如果您有更好的解决方案,请您也解释一下!
感谢您的宝贵时间。
这是我的工作代码:
<?php
function generateList(...$arrays)
{
// Use first array to generate a starting point
$result = [...array_map(function($item){return [$item => []];}, $arrays[0])];
// Adds a new keyed array to the end of every existing empty array
function addToEndChild($object, $string){
if (is_array($object) && count($object) == 0){
return [$string => []];
}
foreach ($object as $index => $item){
$object[$index] = addToEndChild($item, $string);
}
return $object;
};
// Flattens array back down using the keys as values
function flatten($array, $prefix = '', $depth = 0) {
$result = [];
foreach($array as $key => $value) {
if (is_int($key) == false){
$result[] = $key;
}
$flat = flatten($value, $prefix . $key . '.', $depth + 1);
$result = array_merge($result, $depth == 0 ? [$flat] : $flat);
}
return $result;
}
// Loop through remaining arrays and build up nested array
foreach (array_slice($arrays, 1) as $array){
$layer = [];
foreach ($array as $item){
$layer[] = addToEndChild($result, $item);
}
$result = array_merge(...$layer);
}
// Flatten down
$finalResult = flatten($result);
// Convert layers into strings
foreach ($finalResult as $index => $item){
$finalResult[$index] = implode(' ', $item);
}
// Sort alphabetically
asort($finalResult);
return $finalResult;
}
$a = [
'jupiter',
'saturn',
];
$b = [
'is',
'was',
];
$c = [
'old',
'big',
'loved',
];
var_dump(generateList($a, $b, $c));
这是我的代码,只有一个递归函数。而且它以一个矩阵作为参数而不是N个数组。
function generateList($data){
if (count($data) == 0){
return null;
}
$current = $data[0];
$left = array_slice($data, 1);
$permutations = generateList($left);
$r = [];
foreach($current as $item){
if ($permutations){
foreach ($permutations as $rest) {
$r[] = $item . ' ' . $rest;
}
}
else {
$r[] = $item;
}
}
return $r;
}
$data = [
['jupiter', 'saturn'],
['is', 'was'],
['old', 'big', 'loved']
];
var_dump(generateList($data));