重新索引PHP嵌套数组

时间:2018-09-10 16:48:48

标签: arrays multidimensional-array nested

在打印$my_values['outer_group']['fieldset']之后,根据数据,我得到以下输出:

Array
(
 [fieldset] => Array
  (
   [1] => Array
    (
     [title] => Dummy_Value1
     [inner_group] => Array
      (
       [fieldset] => Array
        (
         [1] => Array
          (
           [id] => 11
           [title] => Dummy_Value11
          )

         [2] => Array
          (
           [id] => 12
           [title] => Dummy_Value12
          )

         [3] => Array
          (
           [id] => 13
           [title] => Dummy_Value13
          )

         [actions] => Array
          (
           [add] => Add InnerGroup
           [remove] => Remove InnerGroup
          )

        )

      )

    )

   [2] => Array
    (
     [title] => Dummy_Value2
     [inner_group] => Array
      (
       [fieldset] => Array
        (
         [1] => Array
          (
           [id] => 21
           [title] => Dummy_Value21
          )

         [actions] => Array
          (
           [add] => Add InnerGroup
          )

        )

      )

    )

   [actions] => Array
    (
     [add] => Add OuterGroup
     [remove] => Remove OuterGroup
    )

  )

)

我的要求是重新索引输出数据,因此我执行了以下代码来重新索引相同的数据:

<?php
if (isset($my_values['outer_group']) && !empty($my_values['outer_group'])) {
  $outer_types = $my_values['outer_group']['fieldset'];
  $inner = [];
  foreach ($outer_types as $outer_key => $outer_value) {
    if (is_numeric($outer_key)) {
      if (isset($outer_value['inner_group']['fieldset'])) {
        foreach ($outer_value['inner_group']['fieldset'] as $k => $v) {
          if (is_numeric($k)) {
            $inner[] = [
              'id' => $v['id'],
              'title' => !empty($v['title']) ? $token->replace($v['title']) : NULL,
            ];
          }
        }
      }
      $my_values['outer'][$outer_key] = [
        'title' => !empty($outer_value['title']) ? $token->replace($outer_value['title']) : NULL,
        'inner' => $inner,
      ];
    }
  }
}

根据输出重新索引,但数据中有一些错误。填充[inner]数据时遇到麻烦,以下是相同数据的输出:

Array
(
  [0] => Array
    (
      [title] => Dummy_Value1
      [inner] => Array
        (
          [0] => Array
            (
              [id] => 11
              [title] => Dummy_Value11
            )

          [1] => Array
            (
              [id] => 12
              [title] => Dummy_Value12
            )

          [2] => Array
            (
              [id] => 13
              [title] => Dummy_Value13
            )

        )

    )

  [1] => Array
    (
      [title] => Dummy_Value2
      [inner] => Array
        (
          [0] => Array
            (
              [id] => 11
              [title] => Dummy_Value11
            )

          [1] => Array
            (
              [id] => 12
              [title] => Dummy_Value12
            )

          [2] => Array
            (
              [id] => 13
              [title] => Dummy_Value13
            )

          [3] => Array
            (
              [id] => 21
              [title] => Dummy_Value21
            )

        )

    )

)

应该是:

Array
(
  [0] => Array
    (
      [title] => Dummy_Value1
      [inner] => Array
        (
          [0] => Array
            (
              [id] => 11
              [title] => Dummy_Value11
            )

          [1] => Array
            (
              [id] => 12
              [title] => Dummy_Value12
            )

          [2] => Array
            (
              [id] => 13
              [title] => Dummy_Value13
            )

        )

    )

  [1] => Array
    (
      [title] => Dummy_Value2
      [inner] => Array
        (
          [0] => Array
            (
              [id] => 21
              [title] => Dummy_Value21
            )

        )

    )

)

0 个答案:

没有答案