加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP组合两个或多个数组

发布时间:2020-12-13 16:11:01 所属栏目:PHP教程 来源:网络整理
导读:参见英文答案 How to get all combinations from multiple arrays?????????????????????????????????????2个 嗨大家我不能在PHP之间做.请帮我.. 我有这些数组. $arr1=[9,47]$arr2=[15,113]$arr3=[42,69]//dynamically may be there is more or less array.
参见英文答案 > How to get all combinations from multiple arrays?????????????????????????????????????2个
嗨大家我不能在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,...
            //firstArray_firstValue,thirdArray_secondValue,...
            //...
            //firstArray_firstValue,secondArray_secondValue,...
            //...
            //firstArray_secondValue,...
            //firstArray_secondValue,...

这些数组的示例结果

$resultArray = [
        [9,15,42],[9,69],113,[47,];

如何动态创建此结果?

解决方法

这里是:

<?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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读