Php 循环遍历对象的所有元素
我有一个这样的物体:Php 循环遍历对象的所有元素,php,laravel-5,Php,Laravel 5,我有一个这样的物体: myObj = {"id":12,"title":"my title","content":"my content"} echo " $key ." = ". $value result : id = 12 title = my title content = my content 我想循环遍历此对象的所有元素,以获得如下内容: myObj = {"id":12,"title":"my
myObj = {"id":12,"title":"my title","content":"my content"}
echo " $key ." = ". $value
result : id = 12
title = my title
content = my content
我想循环遍历此对象的所有元素,以获得如下内容:
myObj = {"id":12,"title":"my title","content":"my content"}
echo " $key ." = ". $value
result : id = 12
title = my title
content = my content
但是如果我像这样循环通过我的对象:
foreach ($obj as $key => $value) {
echo "$key = $value\n";
}
我得到了这个结果:
incrementing => 1
exists => 1
wasRecentlyCreated =>
如何获取对象的元素?检查以下代码:
$myObj = '{"id":12,"title":"my title","content":"my content"}';
$object = json_decode($myObj);
foreach($object as $key => $value) {
echo $key . " = " . $value . "<br>";
};
$myObj='{“id”:12,“title”:“我的title”,“content”:“我的content”}';
$object=json_decode($myObj);
foreach($key=>$value的对象){
echo$key.“=”$value.“
”;
};
您可以使用
get_object_vars()
然后使用$someloopedvalfromrarr=$object->{$someloopedvalfromrarr}
循环遍历结果数组,被问题弄糊涂了。。。您得到的结果在myObj
中根本不存在?或者这只是你问题的一个坏例子?