I have a stdclass objects and I want to extract img data from that objects but I have done like below but it is not working.
Below is my code,
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;
}
}
My resultant Array,
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
)
From above array I want to extract only img data ie, images How I can resolve this please help me someone.
$values->design->front contains below array,
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'}}
)
If your $frontdata array is
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/…;}} )
Try this,
foreach ($values->design->front as $front => $frontdata){
foreach($front_data as $val){
echo $val[1]['img'];
}
}
I prefer working with JSON after json_decode
like with array. For it add second argument true
ie json_decode($front_data, true)
Try this:
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'];
}
}
}
you can try to use json_decode
and json_encode
on the object like in this example: 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.