从php对象获取数据 [英] get data from php object
本文介绍了从php对象获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 php 新手,我不知道如何从对象内部的对象获取数据,然后是数组,然后是该数据,所以想从 php 对象获取数据.我用过这个
echo "";var_dump($res);echo "</pre>";
获取对象数据为:
object(stdClass)#1 (1) {[1"]=>对象(标准类)#2(13){[childSemCategories"]=>数组(238){[0]=>对象(标准类)#3(13){[childSemCategories"]=>空值[createdById"]=>整数(850)[创建日期"]=>字符串(19)2011-03-14T17:43:50"[显示名称"]=>string(27)磨料和砂轮"[显示名称路径"]=>string(37) "系统 >> 磨料和砂轮"[实体"]=>空值[id"]=>整数(2210)[图像路径"]=>字符串(37)/images/la/s/c/t/n/ctn9mgug_1pe_0.png"[lastModifiedById"]=>整数(850)["lastModifiedDate"]=>字符串(19)2011-11-01T13:39:16"[parentId"]=>整数(1)[parentSemCategory"]=>对象(标准类)#4(13){[childSemCategories"]=>空值[createdById"]=>整数(1)[创建日期"]=>字符串(19)2011-02-16T18:34:48"[显示名称"]=>string(6) "系统"[显示名称路径"]=>string(6) "系统"[实体"]=>空值[id"]=>整数(1)[图像路径"]=>字符串(0)"[lastModifiedById"]=>整数(1)[lastModifiedDate"]=>字符串(19) "2011-07-13T14:10:50"[parentId"]=>空值[parentSemCategory"]=>空值[状态"]=>字符串(6)活动"}[状态"]=>字符串(6)活动"}[1]=>对象(标准类)#5(13){[childSemCategories"]=>空值[createdById"]=>整数(850)[创建日期"]=>字符串(19)2011-03-14T17:46:06"[显示名称"]=>string(25) "丙烯酸塑料和板材"[显示名称路径"]=>string(35) "System >> Acrylic Plastics & Sheets"[实体"]=>空值[id"]=>整数(2242)[图像路径"]=>string(37) "/images/la/s/w/b/n/wbncmgug_1qa_0.png"[lastModifiedById"]=>整数(850)["lastModifiedDate"]=>字符串(19)2011-11-01T13:41:36"[parentId"]=>整数(1)[parentSemCategory"]=>对象(标准类)#6(13){[childSemCategories"]=>空值[createdById"]=>整数(1)[创建日期"]=>字符串(19)2011-02-16T18:34:48"[显示名称"]=>string(6) "系统"[显示名称路径"]=>string(6) "系统"[实体"]=>空值[id"]=>整数(1)[图像路径"]=>字符串(0)"[lastModifiedById"]=>整数(1)["lastModifiedDate"]=>字符串(19) "2011-07-13T14:10:50"[parentId"]=>空值[parentSemCategory"]=>空值[状态"]=>字符串(6)活动"}[状态"]=>字符串(6)活动"}[2]=>对象(标准类)#7(13){[childSemCategories"]=>空值[createdById"]=>整数(2644)[创建日期"]=>字符串(19)2013-01-14T15:30:24"[显示名称"]=>string(19) "粘合剂和填料"[显示名称路径"]=>string(29) "系统 >> 粘合剂和填料"[实体"]=>空值[id"]=>整数(10155)[图像路径"]=>空值[lastModifiedById"]=>整数(2644)[lastModifiedDate"]=>字符串(19)2013-01-14T15:31:01"[parentId"]=>整数(1)[parentSemCategory"]=>对象(标准类)#8(13){[childSemCategories"]=>空值[createdById"]=>整数(1)[创建日期"]=>字符串(19)2011-02-16T18:34:48"[显示名称"]=>string(6) "系统"[显示名称路径"]=>string(6) "系统"[实体"]=>空值[id"]=>整数(1)[图像路径"]=>字符串(0)"[lastModifiedById"]=>整数(1)["lastModifiedDate"]=>字符串(19) "2011-07-13T14:10:50"[parentId"]=>空值[parentSemCategory"]=>空值[状态"]=>字符串(6)活动"}[状态"]=>字符串(6)活动"}[3]=>对象(标准类)#9(13){[childSemCategories"]=>空值[createdById"]=>整数(1)[创建日期"]=>字符串(19)2011-02-16T18:34:48"[显示名称"]=>string(32) "广告、促销和活动"[显示名称路径"]=>string(42) "系统 >> 广告、促销和活动"[实体"]=>空值[id"]=>整数(2)[图像路径"]=>string(35) "/images/la/s/k/0/4/k04dmgug_2_0.png"[lastModifiedById"]=>整数(850)["lastModifiedDate"]=>字符串(19)2011-11-01T13:41:57"[parentId"]=>整数(1)[parentSemCategory"]=>对象(标准类)#10(13){[childSemCategories"]=>空值[createdById"]=>整数(1)[创建日期"]=>字符串(19)2011-02-16T18:34:48"[显示名称"]=>string(6) "系统"[显示名称路径"]=>string(6) "系统"[实体"]=>空值[id"]=>整数(1)[图像路径"]=>字符串(0)"[lastModifiedById"]=>整数(1)["lastModifiedDate"]=>字符串(19) "2011-07-13T14:10:50"[parentId"]=>空值[parentSemCategory"]=>空值[状态"]=>字符串(6)活动"}[状态"]=>字符串(6)活动"}
这里我想获取数据["显示名称"]=>
解决方案
使用 get_object_vars() 函数.
$data = get_object_vars($res);//数据包含数组值
你可以循环 $data
foreach($data as $a){if(!is_array($a)) $a = get_object_vars($a);foreach($a 作为 $b){//循环和cek while是对象值}}
I am new on php and i dont know how to get data from object inside object then array and after that data so want to get data from php abject. i have use this
echo "<pre>";
var_dump($res);
echo "</pre>";
to get objected data as:
object(stdClass)#1 (1) {
["1"]=>
object(stdClass)#2 (13) {
["childSemCategories"]=>
array(238) {
[0]=>
object(stdClass)#3 (13) {
["childSemCategories"]=>
NULL
["createdById"]=>
int(850)
["createdDate"]=>
string(19) "2011-03-14T17:43:50"
["displayName"]=>
string(27) "Abrasives & Grinding Wheels"
["displayNamePath"]=>
string(37) "System >> Abrasives & Grinding Wheels"
["entities"]=>
NULL
["id"]=>
int(2210)
["imagePath"]=>
string(37) "/images/la/s/c/t/n/ctn9mgug_1pe_0.png"
["lastModifiedById"]=>
int(850)
["lastModifiedDate"]=>
string(19) "2011-11-01T13:39:16"
["parentId"]=>
int(1)
["parentSemCategory"]=>
object(stdClass)#4 (13) {
["childSemCategories"]=>
NULL
["createdById"]=>
int(1)
["createdDate"]=>
string(19) "2011-02-16T18:34:48"
["displayName"]=>
string(6) "System"
["displayNamePath"]=>
string(6) "System"
["entities"]=>
NULL
["id"]=>
int(1)
["imagePath"]=>
string(0) ""
["lastModifiedById"]=>
int(1)
["lastModifiedDate"]=>
string(19) "2011-07-13T14:10:50"
["parentId"]=>
NULL
["parentSemCategory"]=>
NULL
["status"]=>
string(6) "ACTIVE"
}
["status"]=>
string(6) "ACTIVE"
}
[1]=>
object(stdClass)#5 (13) {
["childSemCategories"]=>
NULL
["createdById"]=>
int(850)
["createdDate"]=>
string(19) "2011-03-14T17:46:06"
["displayName"]=>
string(25) "Acrylic Plastics & Sheets"
["displayNamePath"]=>
string(35) "System >> Acrylic Plastics & Sheets"
["entities"]=>
NULL
["id"]=>
int(2242)
["imagePath"]=>
string(37) "/images/la/s/w/b/n/wbncmgug_1qa_0.png"
["lastModifiedById"]=>
int(850)
["lastModifiedDate"]=>
string(19) "2011-11-01T13:41:36"
["parentId"]=>
int(1)
["parentSemCategory"]=>
object(stdClass)#6 (13) {
["childSemCategories"]=>
NULL
["createdById"]=>
int(1)
["createdDate"]=>
string(19) "2011-02-16T18:34:48"
["displayName"]=>
string(6) "System"
["displayNamePath"]=>
string(6) "System"
["entities"]=>
NULL
["id"]=>
int(1)
["imagePath"]=>
string(0) ""
["lastModifiedById"]=>
int(1)
["lastModifiedDate"]=>
string(19) "2011-07-13T14:10:50"
["parentId"]=>
NULL
["parentSemCategory"]=>
NULL
["status"]=>
string(6) "ACTIVE"
}
["status"]=>
string(6) "ACTIVE"
}
[2]=>
object(stdClass)#7 (13) {
["childSemCategories"]=>
NULL
["createdById"]=>
int(2644)
["createdDate"]=>
string(19) "2013-01-14T15:30:24"
["displayName"]=>
string(19) "Adhesives & Fillers"
["displayNamePath"]=>
string(29) "System >> Adhesives & Fillers"
["entities"]=>
NULL
["id"]=>
int(10155)
["imagePath"]=>
NULL
["lastModifiedById"]=>
int(2644)
["lastModifiedDate"]=>
string(19) "2013-01-14T15:31:01"
["parentId"]=>
int(1)
["parentSemCategory"]=>
object(stdClass)#8 (13) {
["childSemCategories"]=>
NULL
["createdById"]=>
int(1)
["createdDate"]=>
string(19) "2011-02-16T18:34:48"
["displayName"]=>
string(6) "System"
["displayNamePath"]=>
string(6) "System"
["entities"]=>
NULL
["id"]=>
int(1)
["imagePath"]=>
string(0) ""
["lastModifiedById"]=>
int(1)
["lastModifiedDate"]=>
string(19) "2011-07-13T14:10:50"
["parentId"]=>
NULL
["parentSemCategory"]=>
NULL
["status"]=>
string(6) "ACTIVE"
}
["status"]=>
string(6) "ACTIVE"
}
[3]=>
object(stdClass)#9 (13) {
["childSemCategories"]=>
NULL
["createdById"]=>
int(1)
["createdDate"]=>
string(19) "2011-02-16T18:34:48"
["displayName"]=>
string(32) "Advertising, Promotions & Events"
["displayNamePath"]=>
string(42) "System >> Advertising, Promotions & Events"
["entities"]=>
NULL
["id"]=>
int(2)
["imagePath"]=>
string(35) "/images/la/s/k/0/4/k04dmgug_2_0.png"
["lastModifiedById"]=>
int(850)
["lastModifiedDate"]=>
string(19) "2011-11-01T13:41:57"
["parentId"]=>
int(1)
["parentSemCategory"]=>
object(stdClass)#10 (13) {
["childSemCategories"]=>
NULL
["createdById"]=>
int(1)
["createdDate"]=>
string(19) "2011-02-16T18:34:48"
["displayName"]=>
string(6) "System"
["displayNamePath"]=>
string(6) "System"
["entities"]=>
NULL
["id"]=>
int(1)
["imagePath"]=>
string(0) ""
["lastModifiedById"]=>
int(1)
["lastModifiedDate"]=>
string(19) "2011-07-13T14:10:50"
["parentId"]=>
NULL
["parentSemCategory"]=>
NULL
["status"]=>
string(6) "ACTIVE"
}
["status"]=>
string(6) "ACTIVE"
}
Here i am want to get data of ["displayName"]=>
解决方案
use get_object_vars() function.
$data = get_object_vars($res); // data contain array value
you can looping $data
foreach($data as $a){
if(!is_array($a)) $a = get_object_vars($a);
foreach($a as $b){
//loop and cek while is object value
}
}
这篇关于从php对象获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文