1

I got this from the name attribute of a field

name="field[a][2][b][0][c][1][field_name]"

after serializing the form, I got this:

array('field[a][2][b][0][c][1][field_name]'=>'value')

and I need to convert that into the following array:

$field = array (
         'a' => array (
                [2] => array (
                       'b' => array (
                              [0] => array (
                                     'c' => array (
                                            [1] => array (
                                                 'field_name'=>'value'
                                            )
                                      )
                               )
                        )
                  )
           )
    );

do I need some sort of foreach function or php can recognize this string as array?

9
  • Did you try this Commented Aug 26, 2018 at 5:56
  • I saw this stackoverflow.com/questions/24440020/…, but I cannot use json_decode because my string does not have commas. Commented Aug 26, 2018 at 5:58
  • I also tried to use the id of the field, which looks like this: id="field-a-2-b-0-c-1-field_name by using (explode('-',$id)); but the problem is that the result is not nested Commented Aug 26, 2018 at 6:01
  • $filed = Array "field[a][2][b][0][c][1][field_name] => value"; dd($filed); Commented Aug 26, 2018 at 6:05
  • thank you dejavuguy, let me try. What is the did command for? Commented Aug 26, 2018 at 6:09

5 Answers 5

5

If you want the result nested, use parse_str().

$text = "field[a][2][b][0][c][1][field_name]=value";
parse_str($text, $result);
print_r($result);

Output:

Array
(
    [field] => Array
        (
            [a] => Array
                (
                    [2] => Array
                        (
                            [b] => Array
                                (
                                    [0] => Array
                                        (
                                            [c] => Array
                                                (
                                                    [1] => Array
                                                        (
                                                            [field_name] => value
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

See https://3v4l.org/7nmFT

Sign up to request clarification or add additional context in comments.

2 Comments

The parse_str function is intended for parsing a query string of an URL. I think that using it in this way may not be very reliable.
It seems to me the this is exactly the use case parse_str was intended for. The string originates from an html text-input attribute name.
1

You can get values in brackets with the regular expression, and then reduce it to the array that you want:

$key = 'field[a][2][b][0][c][1][field_name]';
$value = 'value';

$matches = array();
preg_match_all('/\[([^[]+)\]/', $key, $matches);

$keys = array_reverse($matches[1]);
$result = array_reduce($keys, function ($array, $item) {
    return array($item => $array);
}, $value);

Explanation

In the regular expression \[([^[]+)\]:

  • ([^[]+) is matches any symbol except opening bracket, one or more
    times, and gets it into the capturing group (I hope you will not have nested brackets);
  • \[...\] is literally matches brackets around.

The preg_match_all function should populate the $matches array with following data:

Array
(
    [0] => Array
        (
            [0] => [a]
            [1] => [2]
            [2] => [b]
            [3] => [0]
            [4] => [c]
            [5] => [1]
            [6] => [field_name]
        )

    [1] => Array
        (
            [0] => a
            [1] => 2
            [2] => b
            [3] => 0
            [4] => c
            [5] => 1
            [6] => field_name
        )
)

The $matches[0] have values of a full match and the $matches[1] have values of our first and only capturing group. We have interested only in capturing group values.

Then with the array_reduce function we can simply go through keys in the reverse order, and sequentially wrap our value into an array.

1 Comment

Very nice. I was struggling with the regex.. +1
0

You can use explode function to do it. But the values should have space in between them, for example "Hello World" in which the values would be in Array ( [0] => Hello [1] => world ) but in your case it would be like Array ( [0] => field[a][2][b][0][c][1][field_name] ) until $name as space in-between the characters or word.

$name="field[a][2][b][0][c][1][field_name]"
$name_array = explode(" ",$name);
print_r ($name_array);

2 Comments

thank you Balaji, however that does not make the array nested, right?
yes, it is not. I think Rei, snippets will do the exact job you looking for.
0

When testing your problem on my PHP server with this test code

<form method="post">
<input type="text" name="field[a][2][b][0][c][1][field_name]">
<input type="submit" value="OK">
<form>
<pre>
<?php
if (isset($_POST['field']))
    print_r($_POST['field']);
?>
</pre>

I got the following response (after entering the word "hello" into the text box and clicking the "OK" button):

Array ( [a] => Array ( [2] => Array ( [b] => 
        Array ( [0] => Array ( [c] => Array ( [1] => 
        Array ( [field_name] => hello ) ) ) ) ) ) ) 

Admittedly, it was formatted nicer, but I am posting from my smartphone, so, please, forgive me for not formatting it again manually.

So, to me it is not quite clear why OP needs extra code to solve his/her problem.

Comments

0

Here is your solution: https://codebrace.com/editor/b06588218

I have used regex match twice to match variable name and arrays

/[(.+?)]/ matches any values in the array where "?" is lazy matching. while following regex /^[^[]+/ matches the variable name.

I have used variable variables to create variable from string extracted from above regex

Result:

Array
(
    [a] => Array
        (
            [2] => Array
                (
                    [b] => Array
                        (
                            [0] => Array
                                (
                                    [c] => Array
                                        (
                                            [1] => Array
                                                (
                                                    [field_name] => value
                                                )

                                        )

                                )

                        )

                )

        )

)

I hope this helps

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.