Get Undefined property: stdClass::$key error when use JSON inside of DOM in my plugin

In my WP plugin I use the following code

<?php
$str = ‘{“p_0″:”New first content”}’;
$json = json_decode($str);

$html = ‘<p>First</p><p>Second</p>’;
$dom = new DOMDocument;
$html = mb_convert_encoding($html, ‘HTML-ENTITIES’, ‘UTF-8’);
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_use_internal_errors(false);

foreach($dom->getElementsByTagName(‘p’) as $index => $p) {
$p->setAttribute(‘id’,’p_’.$index);
$id = $p->getAttribute(‘id’);
if($json->{$id}) {
$p->nodeValue = $json->{$id};
}
}

echo $dom->saveXML($dom);
?>

The first paragraph is overriden but at that I get the error

Undefined property: stdClass::$p_0 in …

How can I avoid the error?

Read more here:: Get Undefined property: stdClass::$key error when use JSON inside of DOM in my plugin

Leave a Reply

Your email address will not be published. Required fields are marked *