【问题标题】:How to join 3 variables from 3 arrays with the same labels into a fourth array in php如何将具有相同标签的3个数组中的3个变量连接到php中的第四个数组中
【发布时间】:2011-12-15 07:14:53
【问题描述】:

我有 3 个从动态生成的表单中获取的数组(可能有可变数量的条目,但是这 3 个数组中的每一个的条目数将始终相同):

services[0] => $service1
services[1] => $service2
services[2] => $service3

amount[0] => $amount1
amount[1] => $amount2
amount[2] => $amount3

price[0] => $price1
price[1] => $price2
price[2] => $price3

如何为这些数组的每个变量创建一个数组? 对于具有相同标签的每个服务,我想创建一个名为 service 的数组,如下所示:

Service1[0] => services[0]
Service1[1] => amount[0]
Service1[2] => price[0]

Service2[0] => services[1]
Service2[1] => amount[1]
Service2[2] => price[1]

Service3[0] => services[2]
Service3[1] => amount[2]
Service3[2] => price[2]

并最终制作一个像这样的最终数组:

Services[0] => Service1
Services[1] => Service2
Services[2] => Service3

如果我知道从表单中获得的变量的确切数量,我可以轻松做到这一点,但由于它是动态生成的,我不知道。 谢谢

【问题讨论】:

    标签: php arrays forms join dynamic-data


    【解决方案1】:
    for($i=0;$i<3;$i++){
      $Services[$i]=array($services[$i],$amount[$i],$price[$i]);
    }
    

    在 php 中,变量前需要$

    【讨论】:

    • 这只是一个解释结构的例子。谢谢你的代码,我明白了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2020-07-16
    相关资源
    最近更新 更多