从 PHP 中的深度嵌套数组获取子数组数据

问题描述 投票:0回答:1

我想从深度嵌套的数组中提取数据。我正在寻找一个函数,该函数将产生符合某些条件的数组的某些部分。我正在寻找 'i' => 'p' 子数组下找到的数据 给出下面的嵌套数组:

              [
                'a' => [
                 'i' => [
                  'p' => [
                    'v' => [
                      'age'  => 10,
                      'name' => 'John',
                      'city' => 'Seattle'
                    ],
                    'l' => [
                      'referee' => 'Jane Smith'
                    ],
                    'm' => [
                      'referee' => 'Jane Smith'
                    ],
                  ]
                ]
              ],
              'b' => [
                'i' => [
                  'p' => [
                    'sibling' => 'Peter'
                  ]
                ],
              ],
              'c' => [
                'i' => [
                  'p' => [
                    'material' => [
                      'i' => [
                        'p' => [
                          'marketId' => [
                            'title' => 'marketplaceId'
                          ],
                          'color' => [
                            'i' => [
                              'p' => [
                                'value' => [
                                  'title' => 'Mr',
                                  "type" => "string"
                                ],
                                'language' => 'C++'
                              ]
                            ]
                          ],
                        ],
                      ]
                    ]
                  ]
                ],
              ],
              'd' => [
                'i' => [
                  'p' => [
                    'schedule' => [
                      'i' => [
                        'p' => [
                          'value' => [
                            'title' => 'your value',
                            'description' => 'your description'
                          ]
                        ]
                      ]
                    ]
                  ]
                ]
              ],
              'e' => [
                'i' => [
                  'p' => [
                    'value' => [
                      'title' => 'your value',
                      'description' => 'your description'
                    ],
                    'language' => [],
                    'marketplace' => []
                  ]
                ],
              ]
            ]

我正在寻找类似的东西:

[
 'a' => [
   'v' => [
     'age'  => 10,
     'name' => 'John',
     'city' => 'Seattle'
   ],
   'l' => [
     'referee' => 'Jane Smith'
   ],
   'm' => [
     'referee' => 'Jane Smith'
   ],
 ],
 'b' => [
   'sibling' => 'Peter'
 ],
 'c' => [
   'material' => [
     'marketId' => [
       'title'  => 'marketplaceId'
     ],
     'color' => [
         'value' => [
         'title' => 'Mr',
         "type"  => "string"
       ],
       'language' => 'C++'
     ],
   ]
 ],
 'd' => [
   'schedule' => [
     [
      'value' => [
        'title'       => 'your value',
        'description' => 'your description'
       ]
     ]
   ]
 ],
 'e' => [
   [
     'value' => [
       'title'       => 'your value',
       'description' => 'your description'
     ],
     'language'    => [],
     'marketplace' => []
   ]
 ]
]

我尝试过这个功能:

function getIPData($array, $prefix = '') {
  $return = [];
  foreach ($array as $key => $value) {
    if (!empty($value['i']['p'])) {
      $return = array_merge($return, getIPData($value['i']['p'], $prefix . $key . '_'));
    } else {
      echo "Prefix: $prefix Key: $key \n";
      $return[$prefix . $key] = $value;
    }
  }
  return $return;
}

但它产生以下数组:

Array
(
    [a_v] => Array
        (
            [age] => 10
            [name] => John
            [city] => Seattle
        )

    [a_l] => Array
        (
            [referee] => Jane Smith
        )

    [a_m] => Array
        (
            [referee] => Jane Smith
        )

    [b_sibling] => Peter
    [c_material_marketId] => Array
        (
            [title] => marketplaceId
        )

    [c_material_color_value] => Array
        (
            [title] => Mr
            [type] => string
        )

    [c_material_color_language] => C++
    [d_schedule_value] => Array
        (
            [title] => your value
            [description] => your description
        )

    [e_value] => Array
        (
            [title] => your value
            [description] => your description
        )

    [e_language] => Array
        (
        )

    [e_marketplace] => Array
        (
        )

)

php multidimensional-array nested
1个回答
0
投票

您的代码非常接近实现目标。这是我的方法。

  1. 如果找到,则删除 [i][p] 节点。
  2. 通过递归检查嵌套的 [i][p]。

此外,如果您需要深度控制,您可以添加深度和 maxDepth 来控制嵌套递归行为。

功能:

function getIPData($array) {
    $return = [];
    foreach ($array as $key => $value) {

        if (!empty($value['i']['p'])) {
            $value = $value['i']['p'];
        }

        if (is_array($value)) {
            $value = getIPData($value);
        }

        $return[$key] = $value;
    }
    return $return;
}

输出:

Array
(
    [a] => Array
        (
            [v] => Array
                (
                    [age] => 10
                    [name] => John
                    [city] => Seattle
                )

            [l] => Array
                (
                    [referee] => Jane Smith
                )

            [m] => Array
                (
                    [referee] => Jane Smith
                )

        )

    [b] => Array
        (
            [sibling] => Peter
        )

    [c] => Array
        (
            [material] => Array
                (
                    [marketId] => Array
                        (
                            [title] => marketplaceId
                        )

                    [color] => Array
                        (
                            [value] => Array
                                (
                                    [title] => Mr
                                    [type] => string
                                )

                            [language] => C++
                        )

                )

        )

    [d] => Array
        (
            [schedule] => Array
                (
                    [value] => Array
                        (
                            [title] => your value
                            [description] => your description
                        )

                )

        )

    [e] => Array
        (
            [value] => Array
                (
                    [title] => your value
                    [description] => your description
                )

            [language] => Array
                (
                )

            [marketplace] => Array
                (
                )

        )

)

如有需要请随时沟通

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.