tags:

views:

93

answers:

3

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?

+6  A: 

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)));
soulmerge
@soulmerge, Thanks. Ok, I understand what you mean right now. I need to make operations on array, rather than make operations on json?
garcon1986
that is correct, changing an array is much easier than changing a json string using a regex.
soulmerge
@soulmerge, yes, it's working using 'dump(json_encode(array_values($enterprise)));'. But i think i still need to do some operations on the array. Thanks.
garcon1986
I think, changing an array in not easier than changing json. Terrible!!
garcon1986
I haven't done this by changing an array. But i've done this using the method in my post.
garcon1986
+1  A: 

JSON is not a regular language and thus you can not use regular expressions to parse it. And your expected output is not valid JSON as there cannot be multiple occurrences of the same key like in

{
    "text":"e1_site2_sa1",
    "text":"e1_site2_sa2"
}
Gumbo
@Gumbo, i'm sorry i have made a mistake, i changed it. You can see it in the post.
garcon1986
Minor nitpick: JSON is a way of representing data as a string, and thus you *can* use regex to parse/change it. Of course, it's still a bad idea ;-)
MSpreij
@MSpreij: JSON is *not* regular and thus you can not use regular expressions to parse it. Only specific parts are regular (*string*, *number*, `true`, `false`, and `null`) but not all (*object* and *array*).
Gumbo
@Gumbo: Aah.. I guess I misunderstood the meaning of "regular". Thanks for the correction.
MSpreij
A: 

You should find the way to output the desired json output from json_encode, not by outputting something you don't want and then trick them with regexes. So my guess is that the work is on your data structure rather than on the text output...

Philippe
@Philipe, yes, i think u're right. I'll try to figure it out.
garcon1986