1

I have one array in two format. I want to change array from

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
                )
       )
)

this format to

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
)

these are are one array into two format. i have data in to first array format and i want to change that format to second array type format. please tell me how i am trying this for 2 days but not succeed.

3
  • what code have you tried? please add it to the question. Commented Apr 9, 2016 at 11:29
  • If you use var_export for your array dumps, it will make it easier to provide a solution. Commented Apr 9, 2016 at 11:32
  • @Ryan Vincent echo '<pre>'; // $arr = array(); $formdata = $_POST['formdata']; foreach($formdata as $key=>$val){ $test = $val['name']; $$test = array(); $$test = $val['value']; echo ${$test}; //$arr[] = $$test; //array_push($$test,$val['value']); //echo str_replace('[]','',$$test); die; //print_r($$test); die; //$newarray = array_merge($arr, $$test); //print_r($arr); die; } print_r($arr); print_r($_POST); die('kk'); i have tried this or many more these type but not succed Commented Apr 9, 2016 at 11:59

1 Answer 1

1

Here is a function you could use to produce that conversion:

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;
}

You would call it like this:

$output = convert_formdata($input);

See it run on eval.in for the given input. The output is:

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',
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.