php - 合并多个 Array() 取决于循环和数字

标签 php arrays

这个问题在这里已经有了答案:





merging two set of array values into one multidimesional array

(2 个回答)



Finding cartesian product with PHP associative arrays

(10 个回答)


8年前关闭。




我想合并 3 个数组。

我的第一个是:

Array ( [0] => Leaves-19 [1] => Shifts-1 [2] => Shifts-1 [3] => Shifts-1 [4] => Shifts-1 [5] => Shifts-1 [6] => Leaves-19 [7] => Leaves-19 [8] => Shifts-1 [9] => Shifts-1 [10] => Shifts-1 [11] => Shifts-1 [12] => Shifts-1 [13] => Leaves-19 [14] => Leaves-19 [15] => Shifts-1 [16] => Shifts-1 [17] => Shifts-1 [18] => Shifts-1 [19] => Shifts-1 [20] => Leaves-19 [21] => Leaves-19 [22] => Shifts-1 [23] => Shifts-1 [24] => Shifts-1 [25] => Shifts-1 [26] => Shifts-1 [27] => Leaves-19 [28] => Leaves-19 [29] => Shifts-1 [30] => Shifts-1 [31] => Shifts-1 [32] => Shifts-1 [33] => Shifts-1 [34] => Leaves-19 [35] => Leaves-19 [36] => Shifts-1 [37] => Shifts-1 [38] => Shifts-1 [39] => Shifts-1 [40] => Shifts-1 [41] => Leaves-19 )

我的第二个是:
Array ( [0] => 2013-04-28 [1] => 2013-04-29 [2] => 2013-04-30 [3] => 2013-05-01 [4] => 2013-05-02 [5] => 2013-05-03 [6] => 2013-05-04 )

第三个是:
Array ( [0] => 13 [1] => 10 [2] => 12 [3] => 9 [4] => 14 [5] => 11 )

我要:
  • 2013-04-28/13/Leaves-19
  • 2013-04-29/13/Shifts-1
  • 2013-04-30/13/Shifts-1
  • 2013-05-01/13/Shifts-1
  • 2013-05-02/13/Shifts-1
  • 2013-05-03/13/Shifts-1
  • 2013-05-04/13/Leaves-19
  • 2013-04-28/10/Leaves-19
  • 2013-04-29/10/Shifts-1
  • 2013-04-30/10/类次-1
  • 2013-05-01/10/类次-1
  • 2013-05-02/10/类次-1
  • 2013-05-03/10/类次-1
  • 2013-05-04/10/Leaves-19
  • ...

  • 感谢帮助。

    我尝试了什么:
    echo print_r($_POST['dayType'])."<hr />";
    echo print_r($_POST['dayArr'])."<hr />";
    echo print_r($_POST['userArr'])."<hr />";
    
    //echo count($_POST['dayType'])." --- ".count($_POST['dayArr'])." --- ".count($_POST['userArr']);
    
    // 2013-05-04 / 13 / Leaves-19
    $loopNb1 = count($_POST['dayType']);
    $loopNb2 = count($_POST['dayType'])/7;
    
    for($a=0; $a<$loopNb1; $a++) {
        echo $_POST['dayType'][$a]."<br />";
    }
    
    echo "<hr />";
    
    for($b=0; $b<$loopNb2; $b++) {
        echo $_POST['userArr'][1]."<br />";
    }
    

    最佳答案

    第一个/第二个/第三个数组与您粘贴的相同(顺序相同)。

    $datesCount        = count( $secondArray );
    $firstArrayLength  = count( $firstArray );
    $thirdArrayLength  = count( $thirdArray );
    
    for( $i=0 ; $i < $thirdArrayLength ; $i++ )
    {
        $currentThirdArrayValue = $thirdArray[$i];
    
        for( $inner=0, $firstArrayIndex=0 ; $inner < $datesCount ; $inner++, $firstArrayIndex++ )
        {
            if( $firstArrayIndex == $firstArrayLength )
                    $firstArrayIndex = 0;
    
            echo "{$secondArray[$inner]} / {$currentThirdArrayValue} / {$firstArray[$firstArrayIndex]}<br/>\n";
        }
    }
    

    会给你:
    2013-04-28 / 13 / Leaves-19<br/>
    2013-04-29 / 13 / Shifts-1<br/>
    2013-04-30 / 13 / Shifts-1<br/>
    2013-05-01 / 13 / Shifts-1<br/>
    2013-05-02 / 13 / Shifts-1<br/>
    2013-05-03 / 13 / Shifts-1<br/>
    2013-05-04 / 13 / Leaves-19<br/>
    2013-04-28 / 10 / Leaves-19<br/>
    2013-04-29 / 10 / Shifts-1<br/>
    2013-04-30 / 10 / Shifts-1<br/>
    2013-05-01 / 10 / Shifts-1<br/>
    2013-05-02 / 10 / Shifts-1<br/>
    2013-05-03 / 10 / Shifts-1<br/>
    2013-05-04 / 10 / Leaves-19<br/>
    

    等等......以:
    2013-05-01 / 11 / Shifts-1<br/>
    2013-05-02 / 11 / Shifts-1<br/>
    2013-05-03 / 11 / Shifts-1<br/>
    2013-05-04 / 11 / Leaves-19<br/>
    

    关于php - 合并多个 Array() 取决于循环和数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16377935/

    相关文章:

    python - 如何知道何时使用 numpy.linalg 而不是 scipy.linalg?

    java - 如何创建 2D char 数组的 2D 数组?

    php - 我收到 laravel mysql [2002] 连接错误

    objective-c - 初始化 'Method *' C 时不兼容的指针类型

    php - Laravel 根据关系结果返回数据

    php - 将用户从任何没有 index.php 的文件夹路由到某个文件,例如 : index. php |使用.htaccess

    python - arr.__len__() 是在 Python 中获取数组长度的首选方法吗?

    arrays - 如何在 PowerShell 中比较两个 DataRow 对象数组?

    javascript - 从 PHP 加载值时,textareas 的 autosize.js 中存在错误

    PHP获取MySQL记录时出现 undefined variable 错误