I need to check if an option value from database is a json object.
This funtion following cannot work :
function isJson( string $str, $is_assoc = true ){
json_decode( $str, $is_assoc );
return (json_last_error() == JSON_ERROR_NONE);
}
It´s normal because with an SQL query I got :
a:2:{i:3;a:4:{s:5:"title";s:0:"";s:4:"text";s:503:"...}}
instead of
{ '3' : { 'title' : "", 'text' : "..." }...}}
How can I do to check if this data is a json object and to get this data as object ?
Final goal : Reaplace correclty value for json object in the database without breaking serialized data.
I need to check if an option value from database is a json object.
This funtion following cannot work :
function isJson( string $str, $is_assoc = true ){
json_decode( $str, $is_assoc );
return (json_last_error() == JSON_ERROR_NONE);
}
It´s normal because with an SQL query I got :
a:2:{i:3;a:4:{s:5:"title";s:0:"";s:4:"text";s:503:"...}}
instead of
{ '3' : { 'title' : "", 'text' : "..." }...}}
How can I do to check if this data is a json object and to get this data as object ?
Final goal : Reaplace correclty value for json object in the database without breaking serialized data.
To check if a string from database is like a:2:{i:3;a:4:{s:5:"title";s:0:"";s:4:"text";s:503:"...}}
, you can use the function is_serialized()
To get the data as php array :
if(is_serialized($data_as_string)){
$data_as_array = unserialize( $data_as_string);
}