代码之家  ›  专栏  ›  技术社区  ›  Denoteone

PHP通过组合现有数组中的元素来创建一个新的多维数组

  •  0
  • Denoteone  · 技术社区  · 6 年前

    我正在尝试通过组合通过提交表单创建的数组中的值来创建一个新的多维数组。我不确定实现这一目标的最有效方式是什么。

    起始阵列:

    [Student_Data] => Array
    (
    [mother-lastname] => Trucker
    [mother-firstname] => Cindy
    [father-lastname] => Power
    [father-firstname] => Bob
    [child_0-lastname] => Powers
    [child_0-firstname] => mike
    [add_sib1] => Array
    (
    [0] => Yes
    )
    [child_1-lastname] => Power
    [child_1-firstname] => Anne
    [add_sib2] => Array
    (
    [0] => Yes
    )
    [child_2-lastname] => Power
    [child_2-firstname] => Ted
    [alt-contact-lastname] => Smith
    [alt-contact-firstname] => Tom
    [alt-contact-number] => 777-7777
    )
    

    我想要实现的是:

    [SubmissionData] => Array
    (
      [student] => Array
        (
        [mother-lastname] => Trucker
        [mother-firstname] => Cindy
        [father-lastname] => Power
        [father-firstname] => Bob
        [child_0-lastname] => Powers
        [child_0-firstname] => mike
        [alt-contact-lastname] => Smith
        [alt-contact-firstname] => Tom
        [alt-contact-number] => 777-7777
        ),
      [student] => Array
      (
        [mother-lastname] => Trucker
        [mother-firstname] => Cindy
        [father-lastname] => Power
        [father-firstname] => Bob
        [child_1-lastname] => Power
        [child_1-firstname] => Anne
        [alt-contact-lastname] => Smith
        [alt-contact-firstname] => Tom
        [alt-contact-number] => 777-7777
      ),
      [student] => Array
      (
        [mother-lastname] => Trucker
        [mother-firstname] => Cindy
        [father-lastname] => Power
        [father-firstname] => Bob
        [child_2-lastname] => Power
        [child_2-firstname] => Ted
        [alt-contact-lastname] => Smith
        [alt-contact-firstname] => Tom
        [alt-contact-number] => 777-7777
      )
    )
    

    背景:所以我想先检查add_sib1和add_sib2是否为真,这样我就知道submissionData数组中会有多少个student数组。每个嵌套数组都有很多相同的数据,除了子名称和姓氏。

    问题:这可以通过循环实现吗?或者我需要静态构建阵列,然后将它们推送到submissionData中吗?

    0 回复  |  直到 6 年前
    推荐文章