我在通过对象(stdObject)循环并更改值时出现问题。
会发生什么:
&
等转换为可读字符...... 这就是出了什么问题。我不知道如何将转换后的字符串放回到对象中。
以下是此功能的代码。
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']看起来像这样:
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 ) )
答案 0 :(得分:17)
使用&
符号将变量传递给循环引用。这意味着你要修改实际变量而不是它的副本。
foreach ($test as &$v1) {
foreach ($v1 as &$v2) {
$v2 = html_entity_decode($v2, ENT_QUOTES, "utf-8") . "\n";
}
}
(注意,这仅适用于PHP 5.0及更高版本....但如果您仍在使用PHP4,那么真的需要升级!)
答案 1 :(得分:5)
答案 2 :(得分:1)