1

I would like to convert and array to json in php, I tried firstly to split keys with the '_' separator, but after this I don't know ...

From this input:

[
    "elements_0_image": "",
    "elements_0_content_subtitle": "Subtitle",
    "elements_0_content_title": "Title",
    "elements_0_content_description": "Description",
    "elements_0_content_accordion_0_label": "label",
    "elements_0_content_accordion_0_contenu": "content",
    "elements_0_content_accordion": 1,
    "elements_0_content_button": {
        "title": "",
        "url": "http://test",
        "target": ""
    }
]

To this output:

{
    "elements": [
        {
            "image": "",
            "content": {
                "subtitle": "Subtitle",
                "title": "Title",
                "description": "Description",
                "accordion": [
                    {
                        "label": "label",
                        "contenu": "content"
                    }
                ],
                "button": {
                    "title": "",
                    "url": "http://test",
                    "target": "",
                }
            }
        }
    ],
}

EDIT:

So based from an old question Create variable length array from string

I tried this:

$output = [];
    foreach ( $reversed as $key => $value ) {
        $keys = explode( '_', $key );
        $last = count( $keys ) - 1;
        $step = &$output;
        foreach ( $keys as $k => $ke ) {
            if ( $k == $last ) {
                $step[ $ke ] = $value;
            } else if ( is_array( $step ) ) {
                $step[ $ke ] = [];
            }
            $step = &$step[ $ke ];
        }
    }

but I get

{
            "elements": [
                {
                    "content": {
                        "button": {
                            "title": "",
                            "url": "http://test",
                            "target": "",
                        }
                    }
                }
            ]
        }
1
  • That's not a PHP array. PHP uses => between keys and values, not :. Commented Sep 28, 2022 at 21:45

1 Answer 1

1

Below is the sample method to create multidimensional array in php

<?php
        // Define a multidimensional array
        $contacts = array(
            array(
                "name" => "Peter Parker",
                "email" => "[email protected]",
            ),
            array(
                "name" => "Clark Kent",
                "email" => "[email protected]",
            ),
            array(
                "name" => "Harry Potter",
                "email" => "[email protected]",
            )
        );
        // Access nested value
        echo "Peter Parker's Email-id is: " . $contacts[0]["email"];
        ?>

you can get complete guide from the link as well .

https://www.tutorialrepublic.com/php-tutorial/php-arrays.php

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

1 Comment

How does this create the array from the input that they show?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.