I've got a problem looping trough an object ( stdObject ) and altering a value.
What happens is:
&
etc. to readable characters... Thats what is going wrong. I don't know how to put the converted string back into the object.
Here is the code of this function.
function jsonRequestHandlerUTF8($query) { $id = "0"; $message = errorHandler($id); $a_result = array(); if (mysql_num_rows($query) == 0) { //Empty sql query $id = '1'; $a_result = jSONErrorObject($id); } else { //No error occurred $a_result['ExceptionId'] = $id; $a_result['ExceptionMessage'] = $message; $a_result['Items'] = null; while ($my_result = mysql_fetch_object($query)) { $a_result['Items'][] = $my_result; } $test = $a_result['Items']; foreach ($test as $v1) { foreach ($v1 as $v2) { $v2 = html_entity_decode($v2, ENT_QUOTES, "utf-8") . "\n"; // Here should be code to get the $v2 inside the object again..... } } } $a_result = json_encode($a_result); return $a_result; }
$a_result['Items'] looks like this:
Array ( [0] => stdClass Object ( [idziekmeldingen] => 1 [meldingID] => 13190 [title] => Ziekmelding: Alex [published] => 2011-05-09 [updated] => 2011-05-09 [content] => Per 9-05-2011 heeft Alex zich ziek gemeld. [location] => AP [institute] => CMI [lastCron] => 2011-05-11 11:32:54 ) [1] => stdClass Object ( [idziekmeldingen] => 2 [meldingID] => 12933 [title] => Ziekmelding: Rimmert [published] => 2011-04-26 [updated] => 2011-04-26 [content] => Per 26-04-2011 heeft Rimmer zich ziek gemeld.Met vriendelijke groet,Luciënne Plomp [location] => AP [institute] => CMI [lastCron] => 2011-05-11 11:32:54 ) )
JavaScript's Array#forEach() function lets you iterate over an array, but not over an object. But you can iterate over a JavaScript object using forEach() if you transform the object into an array first, using Object. keys() , Object. values() , or Object.
To iterate through an array of objects in JavaScript, you can use the forEach() method aong with the for...in loop. The outer forEach() loop is used to iterate through the objects array.
Use the &
symbol to pass the variables into the loop by-reference. This will mean you're modifying the actual variable rather than a copy of it.
foreach ($test as &$v1) {
foreach ($v1 as &$v2) {
$v2 = html_entity_decode($v2, ENT_QUOTES, "utf-8") . "\n";
}
}
(note, this only works in PHP 5.0 and up.... but if you're still using PHP4, you really need to upgrade!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With