douwen3973 2019-03-04 14:53
浏览 65
已采纳

从两个数组创建多维数组

I have the following 2 arrays:

 Array 1
(
    [0] => Speed
    [1] => Grade
    [2] => Speed
    [3] => Grade
    [4] => Speed
    [5] => Grade
    [6] => Grade
    [7] => Speed
    [8] => Size
)

Array 2
(
    [0] => 5200 rpm
    [1] => Red
    [2] => 7200 rpm
    [3] => Blue
    [4] => 8900 rpm
    [5] => Green
    [6] => Purple
    [7] => 10000 rpm
    [8] => Big
)

The values are matching each other. For example: Speed - 5200 rpm , Grade - Red and so forth.

I need to make the above like the following :

$collection = array( 

        "Speed" => array (
           5200 rpm,
           7200 rpm,    
           8900 rpm,
           10000 rpm
        ),

        "Grade" => array (
           Red,
           Blue,
           Green,
           Purple
        ),

        "Size" => array (
           Big
        )
     );

It needs to make an array for each label and store the necessary values into the array. I have tried merging and combining and looping. I am going wrong somewhere.

Can someone please help me out.

  • 写回答

3条回答 默认 最新

  • dpizd08264 2019-03-04 15:01
    关注

    Try this code

    $array_1 = array('Speed','Grade','Speed','Grade','Speed','Grade','Grade','Speed','Size');
    $array_2 = array('5200 rpm','Red','7200 rpm','Blue','8900 rpm','Green','Purple','10000 rpm','Big');
    
    foreach($array_1 as $key=>$elm){
        $finalArray[$elm][] = $array_2[$key];
    }
    
    echo("<pre>");
    print_r($finalArray);
    echo("</pre>");
    

    With foreach you can use label for create new array multi-dimensional! Is very easy. I hope I have helped you, for any questions, please comment

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line