I have a data save on sql, and when I import it to php and try to decode into a object, I am finding a little problem because there are many objects inside the main object. The data I have is this:
[{
"other":"{
\"father\":\"[\"father-3\",\"undefined\",\"father-class\"]\",
\"element\":\"[\"element-3\",\"height: 265px;\",\"element-class\"]\"
}",
"type":"login",
"columns":4,
"offsets":0,
"order":"father,facebook,twitter,email",
"father":"{login-title}"
},
{
"other":"{
\"text\":\"[\"text-4\",\"\",\"text-class\"]\",
\"element\":\"[\"element-4\",\"height: 265px;\",\"element-class\"]\"
}",
"type":"text",
"columns":4,
"offsets":2,
"order":"text",
"text":"<p>hola <strong>muchachos </strong>como estáis?</p>"
}]
"Other" is and object inside each object of this array. When I make a json_decode for the array, I get the array without problems, but if I try to get the other object I get NULL. Finally I found a solution, but I am not sure if it is the best (and this is the reason of my question):
$elements = json_decode($value);
foreach($elements as $element)
{
echo "<h1>ELEMENTO</h1>";
var_dump($element);
echo "<h1>ORIGINAL OTHER</h1>";
echo "<textarea>".$element->other."</textarea>";
$element->other = str_replace("\"]", "\\\"]",$element->other);
$element->other = str_replace("[\"", "[\\\"",$element->other);
$element->other = str_replace("\",\"", "\\\",\\\"",$element->other);
$element->other = str_replace("]\\\",\\\"", "]\",\"",$element->other);
$other = json_decode($element->other);
echo "<h1>OTHER</h1>";
echo "<textarea>".$element->other."</textarea>";
var_dump($other);
}
$value is the original data from the data base.
The problem I think is happen is that in the ORIGINAL OTHER the text shown hasn´t the scape backslash for the double quotes -> \", instead what I see is:
{
"father":"["father-3","undefined","father-class"]",
"element":"["element-3","height: 265px;","element-class"]"
}
And for this reason I need to make all of this str_replace that I am not loving, and I think that must to be another way to do it.
I am watching that there is a $depth limit to decode function, I thought it was for decoding recursive objetcs, but I am not sure how it works.
Thank you.