0

I'm having a problem with this. I have a string that looks like this:

coilovers[strut_and_individual_components][complete_strut][][achse]

And i want to convert it to to array that looks like this:

[coilovers] => Array
(
    [strut_and_individual_components] => Array
    (
        [complete_strut]=> Array
        (
            [1] => Array
            (
                [achse] => some_value     
            )
            [2] => Array
            (
                [achse] => some_value     
            )
        )
    )
)

is it possible?

4
  • 3
    Sure, what have you tried? Commented Aug 13, 2014 at 13:18
  • Please post your try to solve the problem so that we can help you Commented Aug 13, 2014 at 13:20
  • preg_match_all("[(.*?)]", $string, $matches); tried something with regex but no luck Commented Aug 13, 2014 at 13:27
  • It is always not necessary to try if you have a question in your mind. Sometimes people are out of ideas to kick a start. Commented Sep 6, 2021 at 13:47

4 Answers 4

2

Here is a quick implementation of a parser that will attempt to parse this string:

$input = 'coilovers[strut_and_individual_components][complete_strut][][achse]'; 

$output = array(); 
$pointer = &$output;

while( ($index = strpos( $input, '[')) !== false) {  
    if( $index != 0)  { 
        $key = substr( $input, 0, $index);
        $pointer[$key] = array();
        $pointer = &$pointer[$key];
        $input = substr( $input, $index);
        continue;
    }
    $end_index = strpos( $input, ']'); 
    $array_key = substr( $input, $index + 1, $end_index - 1);
    $pointer[$array_key] = array();
    $pointer = &$pointer[$array_key];
    $input = substr( $input, $end_index + 1);
}

print_r( $output);

Essentially, we are iterating the string to find matching [ and ] tags. When we do, we take the value within the brackets as $array_key and add that into the $output array. I use another variable $pointer by reference that is pointing to the original $output array, but as the iteration goes, $pointer points to the last element added to $output.

It produces:

Array
(
    [coilovers] => Array
        (
            [strut_and_individual_components] => Array
                (
                    [complete_strut] => Array
                        (
                            [] => Array
                                (
                                    [achse] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)

Note that I've left the implementation of [] (an empty array key) and setting the values in the last index (some_value) as an exercise to the user.

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

Comments

1

Well I've found an another answer for it and it looks like this:

private function format_form_data(array $form_values) {

    $reformat_array = array();
    $matches = array();
    $result = null;

    foreach($form_values as $value) {
        preg_match_all("/\[(.*?)\]/", $value["name"], $matches);
        $parsed_product_array = $this->parse_array($matches[1], $value["value"]);
        $result = array_push($reformat_array, $parsed_product_array);
    }

    return $result;
}

private function parse_array(array $values, $value) {
    $reformat = array();
    $value_carrier_key = end($values);

    foreach (array_reverse($values) as $arr) {
        $set_value_carrier = array($arr => $reformat);
        if($arr == $value_carrier_key) {
            $set_value_carrier = array($arr => $value);
        }
        $reformat = empty($arr) ? array($reformat) : $set_value_carrier;
    }

    return $reformat; 
}

where array $form_values is:

Array
(
   [name] => '[coilovers][strut_and_individual_components][complete_strut][][achse]',
   [value] => 'some_value'
)

Comments

0

No. If you evaluate the string you will get invalid PHP.

If you want to store a PHP Array as string and get it loaded back as PHP Array, have a look at serialize and unserialize functions.

Of course you can build an array from your string, but you'll have to write a parser.

Comments

0

The solution I propose:

function format_form_data(array $data) {

    $matches = array();
    $result = [];

    foreach($data as $key => $value) {
        preg_match_all("/\[(.*?)\]/", $key, $matches);
        $matches = array_reverse($matches[1]);
        $matches[] =  substr( $key, 0, strpos($key, '['));;

        foreach ($matches as $match) {
            $value = [$match=>$value];
        }

        $result = array_replace_recursive($result, $value);
    }

    return $result;
}

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.