【发布时间】:2010-07-13 11:46:43
【问题描述】:
我正在尝试使用 PHP 将一些 xml 转换为 json 对象。
这应该可以工作,但由于某些奇怪的原因它失败了。
有人可以提供一些意见吗?
// Loop Through images and return the right one.
$i = 1;
foreach($page->image as $image) {
if ($i == $_GET['id']) {
echo json_encode(array(
'background' => $image['bgColor'],
'image' => $image['source'],
'caption' => $image['caption']
));
}
$i++;
}
此代码返回以下内容。
{"background":{"0":"000033"},
"image":"0":"0210e849f02646e2f5c08738716ce7e8b3c1169112790078351021245495.jpg"},
"caption": {"0":"Frog"}}
print_r($image['bgColor']); shows 'SimpleXMLElement Object ( [0] => 000033 )'
echo $image['bgColor']; shows '000033'
如何解析 echo 语句而不是 print_r 语句之类的值。为什么这些不同?
【问题讨论】:
-
当您似乎不了解它的实际作用时,为什么还要使用 print_r? print_r() 显示关于变量的信息
-
print_r中不显示命名空间子级的示例在:SimpleXML and print_r() - why is this empty?