du94414 2013-09-20 23:08
浏览 41
已采纳

在PHP中将五个数组合并为五个“组合”数组

I have five different arrays in my program. All of the arrays are of the same length. For this example let's say all of the arrays contain 6 items. The first item array1[0] should be paired with alle the values of the other arrays index 0. So I get an array of all the index 0s, an array with all the index 1 and 2,3,4 and index 5...

How would one do this?

Added more details: I have the following arrays containing information of the items in a shopping_cart.

$nameArray - contains the names of the products in the basket
$productIdArray - contains the id_numbers of the products in the basket
$priceArray - array of the prices for each item in the basket
$quantityArray - array which holds the quantity of each item in the basket

etc. etc.

I'll like to change the output, so I can send a multidimensionel array containing arrays representing a single product each with all of it's values for sending it of in an ajax call...

Hope it makes sense. :)

  • 写回答

2条回答 默认 最新

  • dongwu8050 2013-09-20 23:30
    关注

    I went with only four arrays as it should be more than enough to explain the process. There may be a more elegant solution here but it would require some more thought.

    The main point is I find it easiest to think about problems like this as tables. Your instance is actually relatively simple. You have arrays of rows and you want to convert them to arrays of columns. Check out my solution.

    <?php
    
    $one = array('brown', 'green', 'red', 'yellow', 'orange', 'purple');
    $two = array('cupcake', 'honeycomb', 'icecream', 'chocolate', 'jellybean', 'milkshake');
    $three = array('monday', 'tuesday', 'wednesday', 'thrusday', 'friday', 'saturday');
    $four = array('january', 'february', 'march', 'april', 'august', 'september');
    
    //put all of your arrays into one array for easier management
    $master_horizontal = array($one, $two, $three, $four);
    $master_vertical = array();
    
    foreach ($master_horizontal as $row) {
      foreach ($row as $key => $cell) {
        $master_vertical[$key][] = $cell;
      }
    }
    
    echo "<PRE>";
    print_r($master_vertical);
    

    Returns...

    Array
    (
        [0] => Array
            (
                [0] => brown
                [1] => cupcake
                [2] => monday
                [3] => january
            )
    
        [1] => Array
            (
                [0] => green
                [1] => honeycomb
                [2] => tuesday
                [3] => february
            )
    
        [2] => Array
            (
                [0] => red
                [1] => icecream
                [2] => wednesday
                [3] => march
            )
    
        [3] => Array
            (
                [0] => yellow
                [1] => chocolate
                [2] => thrusday
                [3] => april
            )
    
        [4] => Array
            (
                [0] => orange
                [1] => jellybean
                [2] => friday
                [3] => august
            )
    
        [5] => Array
            (
                [0] => purple
                [1] => milkshake
                [2] => saturday
                [3] => september
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用