0

I try to get a property of an object.

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

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

returns:

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" } } }

but:

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

returns:

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

What's wrong?

AngelEyes
  • 13
  • 3

2 Answers2

0

Judging by your 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"
        }
    }
}

...the color object is in the first array element of dimension.

Either of these methods will work, the choice is yours.

Code: (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"]);

Output:

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"
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Unfortunally not, I am still sitting on the solution... =( – AngelEyes Sep 26 '18 at 02:10
  • Yes, it works, hard & I had to improvise, but finally I have found my way. This is the thing I need: https://3v4l.org/FH4r3 The only I miss, is to ignore $dimension[2], when there is no [2]. I tryed "if isset", but it didn't work.. – AngelEyes Sep 26 '18 at 03:31
  • Just did, thank you so much. I am still struggeling around, how to ignore ["dimension"][2], when there is only [0] and [1]. Do you have a hint for me? – AngelEyes Sep 26 '18 at 03:42
  • Thank you, it looks nice, but too hard for me to understand it. I already have a solution here: https://3v4l.org/2cVWd. Now I try to isset($dimension[2]) ? foreach($dimension[2] as $var3) : $var3 = " "; - but I get "Syntax Error - unexpected T_FOREACH" ... – AngelEyes Sep 26 '18 at 04:08
  • Thank you mickmackusa, I tryed both codes, mine and yours, but in the result fputcsv puts only one value of the array. I your Code echo($subset->displayName) returns "Farbe" and "Ausführung", I need "$varname1 = 'Farbe' and $varname2 = 'Ausführung', $varname3 = 'someothervalue'. – AngelEyes Sep 26 '18 at 05:09
  • Slowly but surely I despair. :'( https://3v4l.org/7VERX When fputcsv, the same value is got everytime... – AngelEyes Sep 26 '18 at 06:35
  • Your `foreach` loop didn't include any processes, it was just accessing the first variation and ending. Please provide the json string that causes the issue in your screenshot so that I can better understand the issue. https://3v4l.org/MJfXt (to keep moderators happy, please remove earlier comments that are not directly related to your question / my answer) – mickmackusa Sep 26 '18 at 08:22
  • I will try it right now. https://3v4l.org/PR3Bo. And I am going to remove the "bad comments". – AngelEyes Sep 26 '18 at 08:46
  • Sorry, I don't really know what to fix. If you say it doesn't work -- how doesn't it work as you expect? What is your expected result in relation to what is generated. – mickmackusa Sep 26 '18 at 12:11
  • Sorry, I see the other comment-text in the URL is gone. However, in the code we see in the output below, that all works fine (RH 47, RH 52, RH 57), but in the CSV Output I still have: http://prntscr.com/kz1f6f, only RH 57 and suddenly the EAN is not one per variation anymore. – AngelEyes Sep 26 '18 at 16:35
  • I can't help you, you aren't showing me the your actual code that causes your issue. Please delete the older comments. – mickmackusa Sep 28 '18 at 14:56
-1

You should turn it into an array. In json_decode ($json, true), add second parameters. In this way, you can get the content in the form of array.

var_dump($data[0]['key_val']);
J.Doe
  • 15
  • 6
  • Which second parameters do I need to add? Do you have an example? – AngelEyes Sep 25 '18 at 03:12
  • If you are not familiar with the coding you write, then there is nothing I can do about it.@AngelEyes – J.Doe Sep 25 '18 at 03:34
  • Unfortunally I do not write this code, and I am not even a coder, but the coder who was writing this, is not here anymore. So I try to fix some buggs by myself... – AngelEyes Sep 26 '18 at 00:48