How can I change this:
[ "Aluno1": 65.8, "Aluno2": 0.3, "Aluno3": 34.9 ]
to have this:
['dafa', 43.00],['adfasdf', 13.00],['cu', 0.00]
maybe changing :
to ,
and ,
to ][
, but how can I do that using str_replace()
?
To use str_replace()
you can wrap what you want to change in % and then apply. Example below;
$old_sentence = "My name is %name%";
$new_sentence = str_replace('%name%', 'ADAM', $old_sentence);
echo $new_sentence;
"My name is ADAM"
Hope this gives you an idea of how to use it.
looks like you need json_decode($array);
so like:
$your_array=[ "Aluno1": 65.8, "Aluno2": 0.3, "Aluno3": 34.9 ];
json_decode($your_array);
But json is so nice to have arrays in why convert it back?
Here is the answer. In fact it's more complicated that simply replace ":" to "," for example. Try this :
$Stringtoreplace = '[ "Aluno1": 65.8, "Aluno2": 0.3, "Aluno3": 34.9 ]';
$NewStringReplace = str_replace('":','",',$Stringtoreplace);
$NewStringReplace = str_replace(', "','],["',$NewStringReplace);
//If you want to change every double quotes to single remove line below
//$NewStringReplace = str_replace('"',"'",$NewStringReplace);
echo $NewStringReplace;
// echo : [ "Aluno1", 65.8],["Aluno2", 0.3],["Aluno3", 34.9 ]
Ok, thank you guys. I got it by using the following code:
$old_sentence = "[ "Aluno1": 65.8, "Aluno2": 0.3, "Aluno3": 34.9 ]";
$new_sentence = str_replace(',', '],[', $old_sentence);
$output = str_replace(':', ',', $new_sentence);
echo $output;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.