试图获取非对象的属性,但这是一个对象

时间:2018-09-25 02:35:11

标签: php json object properties

我尝试获取对象的属性。

function page($json, $name) {
    $data = json_decode($json);
    //$0 = '0';

    $cSKU = '574294403';
    var_dump($data->variations->$cSKU->dimensions->dimension);
}

返回:

array(3) { [0]=> object(stdClass)#5030 (1) { ["color"]=> object(stdClass)#5029 (5) { ["displayName"]=> string(5) "Farbe" ["value"]=> string(12) "schwarz-blau" ["colorHexCode"]=> string(7) "#19181d" ["baseColor"]=> string(7) "schwarz" ["iconId"]=> string(8) "26183061" } } [1]=> object(stdClass)#5032 (1) { ["version"]=> object(stdClass)#5031 (2) { ["displayName"]=> string(11) "Ausführung" ["value"]=> string(8) "RH 48 cm" } } [2]=> object(stdClass)#5034 (1) { ["unknown"]=> object(stdClass)#5033 (3) { ["displayName"]=> string(7) "Unknown" ["value"]=> string(21) "27,5+ Zoll (69,85 cm)" ["name"]=> string(7) "unknown" } } }

但是:

    $cSKU = '574294403';
    var_dump($data->variations->$cSKU->dimensions->dimension->color);
}

返回:

Trying to get property of non-object in ...

怎么了?

2 个答案:

答案 0 :(得分:0)

根据您的var_dump() ...

array(3) { 
    [0]=> object(stdClass)#5030 (1) {
        ["color"]=> object(stdClass)#5029 (5) {
            ["displayName"]=> string(5) "Farbe"
            ["value"]=> string(12) "schwarz-blau"
            ["colorHexCode"]=> string(7) "#19181d"
            ["baseColor"]=> string(7) "schwarz"
            ["iconId"]=> string(8) "26183061"
        }
    }
    [1]=> object(stdClass)#5032 (1) {
        ["version"]=> object(stdClass)#5031 (2) {
            ["displayName"]=> string(11) "Ausführung"
            ["value"]=> string(8) "RH 48 cm"
        }
    }
    [2]=> object(stdClass)#5034 (1) {
        ["unknown"]=> object(stdClass)#5033 (3) {
            ["displayName"]=> string(7) "Unknown"
            ["value"]=> string(21) "27,5+ Zoll (69,85 cm)"
            ["name"]=> string(7) "unknown"
        }
    }
}

...颜色对象位于dimension的第一个数组元素中。

这些方法中的任何一种都可以使用,选择由您选择。

代码:(Demo

$json = '{"variations":{"574294403":{"dimensions":{"dimension":[{"color":{"displayName":"Farbe","value":"schwarz-blau","colorHexCode":"#19181d","baseColor":"schwarz","iconId":"26183061"}},{"version":{"displayName":"Ausf\\u00fchrung","value":"RH 48 cm"}},{"unknown":{"displayName":"Unknown","value":"27,5+ Zoll (69,85 cm)","name":"unknown"}}]}}}}';

$data = json_decode($json);
$cSKU = '574294403';
//var_dump($data->variations->$cSKU->dimensions->dimension);
var_dump($data->variations->$cSKU->dimensions->dimension[0]->color);

echo "\n---\n";

$array = json_decode($json, true);
var_dump($array["variations"][$cSKU]["dimensions"]["dimension"][0]["color"]);

输出:

object(stdClass)#1 (5) {
  ["displayName"]=>
  string(5) "Farbe"
  ["value"]=>
  string(12) "schwarz-blau"
  ["colorHexCode"]=>
  string(7) "#19181d"
  ["baseColor"]=>
  string(7) "schwarz"
  ["iconId"]=>
  string(8) "26183061"
}

---
array(5) {
  ["displayName"]=>
  string(5) "Farbe"
  ["value"]=>
  string(12) "schwarz-blau"
  ["colorHexCode"]=>
  string(7) "#19181d"
  ["baseColor"]=>
  string(7) "schwarz"
  ["iconId"]=>
  string(8) "26183061"
}

答案 1 :(得分:-1)

您应该将其转换为数组。 在json_decode ($json, true)中,添加第二个参数。这样,您可以以数组形式获取内容。

var_dump($data[0]['key_val']);