How to use regular expression in json(PHP) ?

Posted by topdog 172 days ago Questions| text site children All

Hello,

Now i have a json code like this:

{"1":
    {
    "text":"e1",
    "children":
        {
        "1":
            {
            "text":"e1_site1",
            "children":
                {"1":"e1_site1_salarie1_nom"}
            },
        "3":
            {
            "text":"e1_site2",
            "children":
                {
                "3":"e1_site2_sa1",
                "4":"e1_site2_sa2"
                }
            }
        }
    },
"2":
    {
    "text":"e2",
    "children":
        {
        "2":
            {
            "text":"e2_site2",
            "children":
                {
                "2":"e2_site2_salarie2_nom"
                }
            }
        }
    }
}

And i want it to be :

[
    {
    "text":"e1",
    "children":
    [
        {
        "text":"e1_site1",
        "children":
        [
            {
            "text":"e1_site1_salarie1_nom"
            }
        ],
        "text":
            {
            "text":"e1_site2",
            "children":
            [
                {
                "text":"e1_site2_sa1"
                },
                {
                "text":"e1_site2_sa2"
                }
            ]
            }
    ],
    "text":
        {
        "text":"e2",
        "children":
        [
            {
            "text":"e2_site2",
            "children":
            [
                {
                "text":"e2_site2_salarie2_nom"
                }
            ]
            }
        ]
        }

What i have done right now, it's not complete and finished :

$json = json_encode($entreprise);
$mode = array("/{\"[0-9]\":{\"text\"/", "/children\":{\"[0-9]\"/","/,\"[0-9]\":{\"/", "/,\"[0-9]\":\"/","(}})");
$replacement = array("[{\"text\"","children\":[{\"text\"",",\"text\":{\"",",\"text\":\"","}]");
$json = preg_replace($mode, $replacement, $json);
dump($json);

And the current result :

[
    {
    "text":"e1",
    "children":
    [
        {
        "text":"e1_site1",
        "children":
        [
            {
            "text":"e1_site1_salarie1_nom"
            }
        ],
        "text":
            {
            "text":"e1_site2",
            "children":
            [
                {
                "text":"e1_site2_sa1",
                "text":"e1_site2_sa2"
                }
            ]
            }
    ],
    "text":
        {
        "text":"e2",
        "children":
        [
            {
            "text":"e2_site2",
            "children":
            [
                {
                "text":"e2_site2_salarie2_nom"
                }
            ]
            }
        ]
        }

Do you know how to achieve the ideal ouput with regular expression?

Any ideas or suggestions will be appreciated!! Thanks in advance. ;)

Edit:

The output was changed according to soulmerge's recommendation. It's like this:

[
    {
    "text":"e1",
    "children":
        {
        "1":
            {
            "text":"e1_site1",
            "children":
                {
                "1":"e1_site1_salarie1_nom"
                }
            },
        "3":
            {
            "text":"e1_site2",
            "children":
                {
                "3":"e1_site2_sa1",
                "4":"e1_site2_sa2"
                }
            }
        }
    },
    {
    "text":"e2",
    "children":
        {
        "2":
            {
            "text":"e2_site2",
            "children":
                {
                "2":"e2_site2_salarie2_nom"
                }
            }
        }
    }
]

But it still has a long journey to my ideal result.

Does anyone know how to do that?

Discuss Bury


Who Voted for this Question