PHP组合两个或多个数组
大家好,我不能在php上做中间人。请帮帮我 我有这些数组PHP组合两个或多个数组,php,arrays,function,Php,Arrays,Function,大家好,我不能在php上做中间人。请帮帮我 我有这些数组 $arr1=[9,47] $arr2=[15,113] $arr3=[42,69] //dynamically may be there is more or less array. 我想这样创建数组,意思是将所有数组中的每个值组合起来 //firstArray_firstValue, secondArray_firstValue, thirdArray_firstValue, ...
$arr1=[9,47]
$arr2=[15,113]
$arr3=[42,69]
//dynamically may be there is more or less array.
我想这样创建数组,意思是将所有数组中的每个值组合起来
//firstArray_firstValue, secondArray_firstValue, thirdArray_firstValue, ...
//firstArray_firstValue, secondArray_firstValue, thirdArray_secondValue, ...
//...
//firstArray_firstValue, secondArray_secondValue, thirdArray_firstValue, ...
//firstArray_firstValue, secondArray_secondValue, thirdArray_secondValue, ...
//...
//firstArray_secondValue, secondArray_firstValue, thirdArray_firstValue, ...
//firstArray_secondValue, secondArray_firstValue, thirdArray_secondValue, ...
//...
//firstArray_secondValue, secondArray_secondValue, thirdArray_firstValue, ...
//firstArray_secondValue, secondArray_secondValue, thirdArray_secondValue, ...
这些数组的示例结果
$resultArray = [
[9, 15, 42],
[9, 15, 69],
[9, 113, 42],
[9, 113, 69],
[47, 15, 42],
[47, 15, 69],
[47, 113, 42],
[47, 113, 69],
];
如何动态创建此结果?这里是:
<?php
$arr1=[9,47];
$arr2=[15,113];
$arr3=[42,69];
foreach($arr1 as $a1)
foreach($arr2 as $a2)
foreach($arr3 as $a3)
$resultArray[] = [$a1, $a2, $a3];
print_r($resultArray);
特定于您的案例解决方案是:
$resultArray = [];
foreach ($arr1 as $arr1_item){
foreach ($arr2 as $arr2_item) {
foreach ($arr3 as $arr3_item){
$resultArray[] = [
$arr1_item,
$arr2_item,
$arr3_item,
];
}
}
}
[更新]
为大于2的任意数量的阵列添加实现。
数组必须命名为:$arr1、$arr2$arr{N}
<?php
$arr1 = [9, 47];
$arr2 = [15, 113];
$arr3 = [42, 69];
$array_name_pattern = 'arr';
$iterator = 1;
$array_names = [];
while (isset(${'arr' . $iterator})) {
$array_names[] = 'arr' . $iterator;
$iterator++;
}
$resultArray = [];
if (count($array_names) > 1) { // just checking if we have enough arrays
// initiate first elements and all lines
$can = 1; // combinations for each element in first array
// calculate combinations
for ($chi = 2; $chi <= count($array_names); $chi++) {
$can *= count(${'arr' . $chi});
}
// create all lines with first element
// 9 x x
// 9 x x
// 9 x x in our case
// 9 x x
// 47 x x
// 47 x x
// 47 x x
// 47 x x
foreach (${'arr' . 1} as $current_array_item) {
for ($lhi = 1; $lhi <= $can; $lhi++) {
$resultArray[] = [$current_array_item];
}
}
// iterate trough all remained arrays
for ($i = 2; $i <= count($array_names); $i++) {
$gi = 0; // resulting array line number
$can = 1; // combinations for each element of current array
$chi = $i + 1;
// calculate combinations
for ($chi; $chi <= count($array_names); $chi++) {
$can *= count(${'arr' . $chi});
}
while($gi < count($resultArray)) { // repeating for all lines in resulting array
foreach (${'arr' . $i} as $current_array_item) { // get each item in current array
for ($lhi = 1; $lhi <= $can; $lhi++) { // repeat combinations
// 9 15 x
// 9 15 x
// 9 113 x 2nd array, 2 items, 2 combinations per item
// 9 113 x
// 47 x x
// 47 x x
// 47 x x
// 47 x x
$resultArray[$gi][] = $current_array_item;
$gi++;
}
}
}
}
}
print_r($resultArray);
您知道构建该结果的逻辑,对吗?所以你可以试着自己做一些事情。到目前为止,您尝试了什么?您可能需要使用loopstry array\u merge或array\u map,也许其中一个对您有用。@FelippeDuarte我只在foreach循环中尝试了foreach循环。但我无法找到解决办法。我无法解决我应该建立什么样的结构。@PatrickQ谢谢。它解决了我的问题…谢谢你的回答。但是,如果有更多或更少的数组,则此解决方案不可用。谢谢您的回答。但是,如果数组的数量或多或少,这个解决方案就不可用了。当然,您可以按变量名进行迭代,但这根本不是一个好的做法,您还需要在变量名中设置一些预设模式,或者至少将所有数组名保存在一些额外的数组中。