Search find and replace within string in php

0

Friends good day. today I come with another question, I have a string of the following form.

{"idc100":"1",
"creado_por":"superadmin",
"asignado":"Yhowan Ramirez",
"ips":"["melgar","corondo"]",
"numero":"200116001232201602345",
"devolucion":"["porque si","porque no"]"}

and I need to find these values that are within [] example ["melgar","corondo"] and format it so that it is simply like this: melgar - corondo , in order that the string is like this:

{"idc100":"1",
 "creado_por":"superadmin",
 "asignado":"Yhowan Ramirez",
 "ips":" melgar - corondo",
 "numero":"200116001232201602345",
 "devolucion":"porque si - porque no"}

Some little hands thank you, thank you!

    
asked by Mauricio Delgado 09.07.2018 в 18:33
source

1 answer

1

You have passed the double quotes next to the brackets "[]" this causes a syntax error.

The correct format would be:

{"idc100":"1",
"creado_por":"superadmin",
"asignado":"Yhowan Ramirez",
"ips":["melgar","corondo"],
"numero":"200116001232201602345",
"devolucion":["porque si","porque no"]}

Already with this the following script could do what you need:

<?php

header('Content-Type: application/json');

$json = '{"idc100":"1",
"creado_por":"superadmin",
"asignado":"Yhowan Ramirez",
"ips":["melgar","corondo"],
"numero":"200116001232201602345",
"devolucion":["porque si","porque no"]}';

$data_array = json_decode($json, true);

$data_array["ips"] = implode( ' - ', $data_array["ips"] );
$data_array["devolucion"] = implode( ' - ', $data_array["devolucion"] );

$json = json_encode($data_array);

echo $json;
    
answered by 09.07.2018 в 19:23