如何使用php从stdclass对象提取数据
我有一个stdclass对象,我想从该对象中提取img数据,但我已经像下面这样做了,但它不起作用 下面是我的代码如何使用php从stdclass对象提取数据,php,arrays,json,Php,Arrays,Json,我有一个stdclass对象,我想从该对象中提取img数据,但我已经像下面这样做了,但它不起作用 下面是我的代码 foreach ($values->design->front as $front => $frontdata){ $front_data = str_replace("'", '"',$frontdata); $front_values = json_decode($front_data); // //e
foreach ($values->design->front as $front => $frontdata){
$front_data = str_replace("'", '"',$frontdata);
$front_values = json_decode($front_data);
// //echo $front_values[1]['id'];
// echo "<pre>";print_r($front_values);
// //echo $front_data[1]['img'];
foreach($front_values as $val){
//echo "<pre>";print_r($val);
echo $val->img;
}
}
从上面的阵列中,我只想提取img数据,即图像,我如何解决这个问题,请帮助我
$values->design->front包含以下数组
foreach ($values->design->front as $front => $frontdata){
foreach($front_data as $val){
echo $val[1]['img'];
}
}
如果您的$frontdata数组是
foreach ($values->design->front as $front => $frontdata){
$front_data = str_replace("'", '"',$frontdata);
$front_values = json_decode($front_data, true);
// //echo $front_values[1]['id'];
// echo "<pre>";print_r($front_values);
// //echo $front_data[1]['img'];
foreach($front_values as $val){
//echo "<pre>";print_r($val);
if(isset($val['img'])) {
echo $val['img'];
}
}
}
试试这个,
$result =[];
//first encode and then decode, to not get any errors
$array = json_decode(json_encode($frontdata), true);
var_dump( $array );
foreach($array as $val){
if(isset($val['img'])){
//save into a result array
$result[]=$val['img'];
}
}
var_dump($result);
我更喜欢在
JSON\u decode
之后使用JSON,就像使用数组一样。对于它,添加第二个参数true
,即json\u decode($front\u data,true)
试试这个:
foreach($values->design->front as$front=>$frontdata){
$front_data=str_replace(“'”、“'”、$frontdata);
$front\u values=json\u decode($front\u data,true);
////echo$front_值[1]['id'];
Array
(
[0] => {'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-f127a3f7144332566936559141210548143.png'}}
)
Array
(
[0] => {'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-f127a3f7144332566936559141210548143.png'}}
[1] => {'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-464d828b14433256696212312871108713.png'}}
[2] => {'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-ee23e7ad144332566992049430410306091.png'}}
)
//echo”“;print_r($front_值);
stdClass Object
(
[id] => area-design
)
stdClass Object
(
[id] => images-1
[width] => 500px
[height] => 500px
[top] => 0px
[left] => 0px
[zIndex] => auto
[img] => http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-f127a3f7144332566936559141210548143.png
)
stdClass Object
(
[id] => area-design
)
stdClass Object
(
[id] => images-1
[width] => 500px
[height] => 500px
[top] => 0px
[left] => 0px
[zIndex] => auto
[img] => http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-f127a3f7144332566936559141210548143.png
)
stdClass Object
(
[id] => area-design
)
stdClass Object
(
[id] => images-1
[width] => 500px
[height] => 500px
[top] => 0px
[left] => 0px
[zIndex] => auto
[img] => http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-464d828b14433256696212312871108713.png
)
stdClass Object
(
[id] => area-design
)
stdClass Object
(
[id] => images-1
[width] => 500px
[height] => 500px
[top] => 0px
[left] => 0px
[zIndex] => auto
[img] => http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-ee23e7ad144332566992049430410306091.png
)
////echo$front_data[1]['img'];
Array
(
[0] => {'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-f127a3f7144332566936559141210548143.png'}}
)
Array
(
[0] => {'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-f127a3f7144332566936559141210548143.png'}}
[1] => {'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-464d828b14433256696212312871108713.png'}}
[2] => {'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-ee23e7ad144332566992049430410306091.png'}}
)
foreach($front_值为$val){
//回音“;打印($val);
Array
(
[0] => {'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-f127a3f7144332566936559141210548143.png'}}
)
Array
(
[0] => {'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-f127a3f7144332566936559141210548143.png'}}
[1] => {'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-464d828b14433256696212312871108713.png'}}
[2] => {'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http://192.168.1.156/dutees/tshirtecommerce//uploaded/products/dg-designer-ee23e7ad144332566992049430410306091.png'}}
)
如果(isset($val['img'])){
echo$val['img'];
}
}
}
您可以尝试在对象上使用json\u decode
和json\u encode
,如本例所示:
你能提供
$front\u data
中包含的内容吗?你是如何得到这个数组的?在开始循环之前,使用json\u decode
并显示结果。你可以向我们显示echo”“;print\r($values->design->front);
的确切输出吗?在第一个foreach()之前添加它
并将输出粘贴到question@Timur$front_数据包含=>“{”0:{”id:“区域设计”},“1:{”id:“images-1”,“width:“500px”,“height:“500px”,“top:“0px”,“left:“0px”,“zIndex:“auto”,“img:”}“@alive to die down是输出,数组([0]=>{0':{id':“区域设计”},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':''}它显示为foreach提供的参数无效。它显示无法将stdclass的对象转换为arrayDid。您将true
添加到json\u解码($front\u data,true)
?