Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 592643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:45:40+00:00 2026-05-13T15:45:40+00:00

Now i have a json code like this: {1: { text:e1, children: { 1:

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:45:40+00:00Added an answer on May 13, 2026 at 3:45 pm

    Regular expressions are the worst approach you can use here:

    • You will have trouble understanding what you did if you come back to this code in 3 months,
    • It is easy to make errors that are hard to spot,
    • The code will break if PHP decides to change its json generation code

    I would recommend making all operations on the $entreprise array without taking any risks and then calling json_encode() on it.

    The reason the generated JSON has objects instead of arrays is that the original array is not 0-indexed. Just try this, for example:

    dump(json_encode(array_values($enterprise)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from this question I now have code that can attach to a
I have index.php like this: <script type=text/javascript src=jquery-1.4.2.js></script> <script type=text/javascript src=ajax.js></script> <a href='one.php' class='ajax'>One</a>
[I have some code to create a JSON array. In this code I am
Imagine the following REBOL code: foo: context [bar: 3] I now have a context
I've always wondered about this ... and thankfully, now have a good environment of
Now I have seen this question in another forum but it didn't had an
Coding Platform: ASP.NET C# I have an XML like this. <Items> <Map id=35> <Terrains>
I now have it set up so that when people go to a thank
Modern ATL/MFC applications now have access to a new shared pointer class called CAutoPtr,
I'm a ClearCase newbie and up until now have been used to SVN. Therefore,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.