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 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?