dongshan4316 2017-05-29 08:47
浏览 29
已采纳

在php中从多个多维数组创建一个数组

I have created following array. But this is not what actually i want. help to create a new array from this to fulfill my requirements.

Array (
[supplier_id] => 1
[filter_name] => Array
    (
        [0] => product 1 
        [1] => Product 2
        [2] => Product 3
    )

[quantity] => Array
    (
        [0] => 3
        [1] => 10
        [2] => 2
    )

[unit] => Array
    (
        [0] => pkts
        [1] => ltrs
        [2] => pkts
    )

[price] => Array
    (
        [0] => 11
        [1] => 100
        [2] => 10
    )

[gross_amount] => Array
    (
        [0] => 33
        [1] => 1000
        [2] => 20
    )

[vat] => Array
    (
        [0] => 0
        [1] => 0
        [2] => 0
    )

[net_amount] => Array
    (
        [0] => 33
        [1] => 1000
        [2] => 20
    ) )

And This is the array i want exactly. Is there any idea to create this type of array.

Array(
[supplier_id] = 1
[0] = Array(
    [filter_name] => product 1
    [quantity] => 3
    [unit] => pkts
    [price] => 11
    [gross_amount] => 33
    [vat] => 0
    [net_amount] => 33
)
[1] = Array(
    [filter_name] => product 2
    [quantity] => 10
    [unit] => ltrs
    [price] => 100
    [gross_amount] => 1000
    [vat] => 0
    [net_amount] => 1000
)

[2] = Array(
    [filter_name] => product 3
    [quantity] => 2
    [unit] => pkts
    [price] => 10
    [gross_amount] => 20
    [vat] => 0
    [net_amount] => 20
))
  • 写回答

1条回答 默认 最新

  • dongzhenju3015 2017-05-29 09:29
    关注

    I think this should help you, I am assuming there are maximum 3 values in your sub-array, So I used while($i<3);

    $i = 0;
       $newArray = array();
       do{
       foreach($arr as $key=>$val){
           if($key == 'supplier_id'){
                $newArray[$key] = $val;
            }else{
                $newArray[$i][$key] = $val[$i];
            }
    
       }
       $i++;   
       }while($i<3);
       echo "<pre>"; print_r($newArray);
    

    Check Output here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能