使用键路径值扩展多维数组以具有嵌套子数组

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

我有一个具有两种格式的数组。我想从

更改数组
Array
(
    [step_number] => 4
    [app_id] => Array
        (
            [0] => 2
            [1] => 3
        )
    [formdata] => Array
        (
            [0] => Array
                (
                    [name] => app_id[]
                    [value] => 2
                )
            [1] => Array
                (
                    [name] => app_id[]
                    [value] => 3
                )
            [2] => Array
                (
                    [name] => fieldval[2][2][]
                    [value] => 1
                )
            [3] => Array
                (
                    [name] => fieldval[3][3][]
                    [value] => 200
                )
            [4] => Array
                (
                    [name] => fieldval[3][3][]
                    [value] => day
                )
            [5] => Array
                (
                    [name] => title
                    [value] => new plan
                )
            [6] => Array
                (
                    [name] => feature_plan
                    [value] => 3
                )
            [7] => Array
                (
                    [name] => plan_type
                    [value] => free
                )
            [8] => Array
                (
                    [name] => price
                    [value] => 
                )
            [9] => Array
                (
                    [name] => sell_type
                    [value] => us
                )
       )
)

此格式为

Array
(
    [app_id] => Array
        (
            [0] => 2
            [1] => 3
        )

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

                )

            [3] => Array
                (
                    [3] => Array
                        (
                            [0] => 200
                            [1] => day
                        )

                )

        )

    [title] => new plan
    [feature_plan] => 3
    [plan_type] => free
    [price] => 
    [sell_type] => us
)

这些是一个数组,分为两种格式。我有第一个数组格式的数据,我想将该格式更改为第二个数组类型格式。 请告诉我我是如何尝试了两天但没有成功的。

php arrays multidimensional-array
1个回答
1
投票

这是一个可用于生成该转换的函数:

function convert_formdata($input) {    
    $output = array();
    foreach($input['formdata'] as $data) {
        $keys = preg_split("#[\[\]]+#", $data['name']);
        $value = $data['value'];
        $target = &$output;
        foreach($keys as $key) {
            // Get index for "[]" reference
            if ($key == '') $key = count($target);
            // Create the key in the parent array if not there yet
            if (!isset($target[$key])) $target[$key] = array();
            // Move pointer one level down the hierarchy
            $target = &$target[$key];
        }
        // Write the value at the pointer location
        $target = $value;
    }
    return $output;
}

你可以这样称呼它:

$output = convert_formdata($input);

查看它在给定输入的 eval.in 上运行。输出是:

array (
  'app_id' => 
  array (
    0 => 2,
    1 => 3,
  ),
  'fieldval' => 
  array (
    2 => 
    array (
      2 => 
      array (
        0 => 1,
      ),
    ),
    3 => 
    array (
      3 => 
      array (
        0 => 200,
        1 => 'day',
      ),
    ),
  ),
  'title' => 'new plan,',
  'feature_plan' => 3,
  'plan_type' => 'free',
  'price' => NULL,
  'sell_type' => 'us',
)
© www.soinside.com 2019 - 2024. All rights reserved.