0

I have following js array using serialisedArray -

Array
(
    [0] => Array
        (
            [name] => sub_maintenance_template[1][maintenance_location_id]
            [value] => 54321
        )

    [1] => Array
        (
            [name] => sub_maintenance_template[1][maintenance_problem_id]
            [value] => 65432
        )

    [2] => Array
        (
            [name] => sub_maintenance_template[1][maintenance_priority_id]
            [value] => 76896
        )

    [3] => Array
        (
            [name] => sub_maintenance_template[1][description]
            [value] => sample description
        )
)

Expected array -

[sub_maintenance_template] => Array (
    [1] =>
        (
            [maintenance_location_id]=> 54321
            [maintenance_problem_id]=> 65432
            [maintenance_priority_id]=>76896
            [description]=> sample description
        )
)

I tried like this-

foreach( $tableData as $key => $value ) {
        echo $key;
        $newArray['sub_maintenance_template'][3][] = $value['name'];
        $newArray['sub_maintenance_template'][3][] = $value['value'];
    } 

Even though I iterate it through foreach but failed to get desired output. IS there any way to get desired one?

2
  • I'm not sure why you are tagging JavaScript and jQuery. Commented Apr 9, 2018 at 17:35
  • Did you give up??? Commented Apr 11, 2018 at 20:19

1 Answer 1

1

It would be better to pass these as actual arrays in GET or POST, but since the string in name is how arrays would be passed in a URL query string, you can use parse_str:

foreach($array as $values) {
    parse_str("{$values['name']} = {$values['value']}", $result);
}
print_r($result);

Or another way; extract and build key/value pairs to build a query string and then parse it:

parse_str(http_build_query(array_column($array, 'value', 'name')), $result);
print_r($result);
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.