HK007 HK007 - 11 months ago 43
PHP Question

Keep only one square bracket in php string

My php code is giving result in double brackets like this:

[["Faisalabad Belt",11.428571428571]],

But I want to get result only in single square bracket like this:

["Faisalabad Belt",11.428571428571],

I have tried using str_replace but it is not giving me the required results. It either replace bracket from one side or the other but not properly replaces from both sides. The code I'm using is as follows:

<?php echo (str_replace('[[','[',json_encode($poster))) ; ?>,

Sorry if you find the question clear.


str_replace also accepts arrays... One call and get all of them replaced at the same time

<?php echo (str_replace(['[[', ']]'], ['[', ']'], json_encode($poster))) ;  ?>