[英]while decoding and getting json value i am getting error?
In productoptions coloumn in database table , i insert the value like below在数据库表的productoptions 列中,我插入如下值
[{"value":["Color","size",""]},{"Color":["Red","Blue","white"],"size":["L","XL"]},{"type":["Dropdown","Checkbox",""]}]
After that i got the option values之后我得到了选项值
$opt =$value->ProductOptions;
Then i decode the json value by using following然后我使用以下方法解码 json 值
$jsonvalue = json_decode($opt);
then i got value like below.然后我得到了如下价值。
Array ( [0] => stdClass Object ( [value] => Array ( [0] => Color [1] => size [2] => ) ) [1] => stdClass Object ( [Color] => Array ( [0] => Red [1] => Blue [2] => white ) [size] => Array ( [0] => L [1] => XL ) ) [2] => stdClass Object ( [type] => Array ( [0] => Dropdown [1] => Checkbox [2] => ) ) )
After decoding i am trying to get particular object(color, size etc)解码后,我试图获取特定对象(颜色、大小等)
foreach ($jsonvalue as $key => $values) {
print_r($vales->Color);
}
But i am error like below:但我的错误如下:
Message: Undefined property: stdClass::$Color
消息:未定义的属性:stdClass::$Color
I am get value我得到了价值
value => color,size
After that in need pass above value dynamically and get之后需要动态传递以上值并获得
color => red,blue,white
The JSON structure is a bit odd, you have mixed data and structures. JSON 结构有点奇怪,你有混合的数据和结构。
But if you use print_r($jsonvalue);
但是如果你使用
print_r($jsonvalue);
you can see how the arrays and objects are laid out, this shows that the [0]
item has the value
element, and the [1]
has the data indicated by the values, so...您可以看到数组和对象的布局方式,这表明
[0]
项具有value
元素,而[1]
具有由值指示的数据,因此...
One of the items in your value
element is ""
, this can cause problems so you may want to add a check to ignore empty items. value
元素中的一项是""
,这可能会导致问题,因此您可能需要添加检查以忽略空项。
foreach ( $jsonvalue[0]->value as $types ) {
if ( !empty($types) ) {
echo $types.PHP_EOL;
print_r($jsonvalue[1]->{$types});
}
}
gives you...给你...
Color
Array
(
[0] => Red
[1] => Blue
[2] => white
)
size
Array
(
[0] => L
[1] => XL
)
Try :尝试 :
foreach ($jsonvalue as $key => $values) {
print_r($vales->{$key});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.