Cain Nuke Cain Nuke - 5 months ago 21
JSON Question

Insert additional code in loaded json data


I am loading some data in json but I need to insert some additional data based on the data that is loaded. This is an example of the data as it is loaded

{"markup":"<li id=\"com1\">some content</li><li id=\"com2\">some more content</li>"}

and this is the expected final result:

<li id="com1">some content <a href="delete=com1">DELETE</a></li>
<li id="com2">some content <a href="delete=com2">DELETE</a></li>

as you may see, I'm adding a delete link which has an URL that contains the ID of the element so I need a code to first find that ID and then add it into the right place for each element.

This is my php code:

$json = file_get_contents("data.txt");
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($json, TRUE)),

foreach ($jsonIterator as $key => $val) {
echo $val;

whats the best and simplest way to do it?

Thank you.

Dan Dan

There's php module for parsing html content. This might not be the answer you looking for. But this will give you some idea.

$str = file_get_contents("data.txt");
$data = json_decode($str,true);
$html = $data["markup"];
$dom = new DOMDocument;
$lis = $dom->getElementsByTagName('li');
foreach ($lis as $li) {
    $a = $dom->createElement('a','DELETE');
$final = $dom->saveHTML();
echo $final;