Tony Montana
Tony Montana

Reputation: 1019

Remove instance of square brackets, if found two successive instances

I have a data in following manner:

{"id": "sugarcrm", "text": "sugarcrm", "children": [ [ { "id": "accounts", "text": "accounts", "children": [ { "id": "id", "text": "id" }, { "id": "name", "text": "name" } ] } ] ] }

Now, I want to remove the instance of square bracket i.e. [ and ] if there are two successive instances like this [ [ or ] ].

Now if you see the above data, you'll get to see that there are instances of [ and ] which are repeated twice successively. So I want to remove one instance of each.

Now, I can check the two successively repeated instances of each and remove one, like this

$text = '{"id": "sugarcrm", "text": "sugarcrm", "children": [ [ { "id": "accounts", "text": "accounts", "children": [ { "id": "id", "text": "id" }, { "id": "name", "text": "name" } ] } ] ] }';

echo preg_replace('/\[ \[+/', '[', $text);

Now, the above code is for [. So to remove the successively repeated instance of ], I'll have to repeat the same code again.

I want to know, is there a better way to achieve the same result or not. Meanwhile, I can work it out, but still what if, in future, I'll have to do the same for any other character? Kindly guide me here.

Upvotes: 0

Views: 126

Answers (2)

mickmackusa
mickmackusa

Reputation: 47992

You are processing a json string. It is contraindicated to attempt string manipulations (with regex or other) because there are very possible pitfalls with "over-matching".

While I don't fully understand the variability of your data structure, I can provide some temporary guidance by converting your json string to an array and then safely modifying the data with an array function.

Consider this:

Code: (Demo)

$json='{"id": "sugarcrm", "text": "sugarcrm", "children": [ [ { "id": "accounts", "text": "accounts", "children": [ { "id": "id", "text": "id" }, { "id": "name", "text": "name" } ] } ] ] }';
$array=json_decode($json,true);  // convert to array
foreach($array as &$a){  // $a is modifiable by reference
    if(is_array($a) && isset($a[0]) && isset($a[0][0])){  // check if array and if two consecutive/nested indexed subarrays
        $a=array_column($a,0); // effectively shift deeper subarray up one level
    }
}
$json=json_encode($array);
echo $json;

Output:

{"id":"sugarcrm","text":"sugarcrm","children":[{"id":"accounts","text":"accounts","children":[{"id":"id","text":"id"},{"id":"name","text":"name"}]}]}

For that matter, if you know where the double-nested-indexes are, then you can access them without looping (or modifying by reference) like this:

$json='{"id": "sugarcrm", "text": "sugarcrm", "children": [ [ { "id": "accounts", "text": "accounts", "children": [ { "id": "id", "text": "id" }, { "id": "name", "text": "name" } ] } ] ] }';
$array=json_decode($json,true);
$array['children']=array_column($array['children'],0);  // modify 2 known, nested, indexed subarrays
$json=json_encode($array);
echo $json;

Upvotes: 5

Vijay Rathore
Vijay Rathore

Reputation: 603

How about:

echo str_replace(array('[ [', '] ]'), array('[', ']'), $text);

Upvotes: -1

Related Questions