我有一个stdclass对象,我想从这些对象中提取img数据,但是我已经完成了以下操作,但它没有工作。
以下是我的代码,
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;
}
}
我的结果数组,
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
)
从上面的数组我想只提取img数据,图像如何解决这个问题,请帮助我。
$ values-&gt; design-&gt; front包含在数组下面,
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'}}
)
答案 0 :(得分:0)
如果你的$ frontdata数组是
Array ( [0] => {'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'192.168.1.156/dutees/tshirtecommerce//uploaded/products/…;}} )
试试这个,
foreach ($values->design->front as $front => $frontdata){
foreach($front_data as $val){
echo $val[1]['img'];
}
}
答案 1 :(得分:0)
我更喜欢在json_decode
之后使用JSON。为此添加第二个参数true
,即json_decode($front_data, true)
试试这个:
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'];
}
}
}
答案 2 :(得分:0)
您可以尝试在对象上使用json_decode
和json_encode
,如下例所示:https://eval.in/803391
$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);