0

I am trying to form variables out of an output received from a web service. The web service provides detailed information regarding a vehicle after entering the vehicle's VIN into the system. Below is a portion of the output one receives after entering a VIN.

object(stdClass)#2 (14) {
  ["responseStatus"]=>
  object(stdClass)#3 (2) {
  ["responseCode"]=>
  string(10) "Successful"
  ["description"]=>
  string(10) "Successful"
}
["vinDescription"]=>
object(stdClass)#4 (11) {
  ["WorldManufacturerIdentifier"]=>
  string(17) "Germany Audi Nsu "
 }

["vin"]=>
string(17) "WAUUL78E38A092113"
["modelYear"]=>
int(2008)
["division"]=>
string(4) "Audi"
["modelName"]=>
string(2) "S4"
["styleName"]=>
string(13) "5dr Avant Wgn"
["bodyType"]=>
string(11) "Wagon 4 Dr."
["drivingWheels"]=>
string(3) "AWD"
["builddata"]=>
string(2) "no"
}

["style"]=>
array(2) {
  [0]=>
  object(stdClass)#26 (17) {
   ["division"]=>
  object(stdClass)#27 (2) {
    ["_"]=>
    string(4) "Audi"
    ["id"]=>
    int(4)
  }
  ["subdivision"]=>
  object(stdClass)#28 (2) {
    ["_"]=>
    string(4) "Audi"
    ["id"]=>
    int(5020)
  }
  ["model"]=>
  object(stdClass)#29 (2) {
    ["_"]=>
    string(2) "S4"
    ["id"]=>
    int(17308)
  }
  ["basePrice"]=>
  object(stdClass)#30 (4) {
    ["unknown"]=>
    bool(false)
    ["invoice"]=>
    float(46137)
    ["msrp"]=>
    float(49610)
    ["destination"]=>
    float(775)
  }
  ["bodyType"]=>
  object(stdClass)#31 (3) {
    ["_"]=>
    string(13) "Station Wagon"
    ["id"]=>
    int(7)
    ["primary"]=>
    bool(true)
  }
  ["marketClass"]=>
  object(stdClass)#32 (2) {
    ["_"]=>
    string(11) "Small Wagon"
    ["id"]=>
    int(53)
  }
  ["acode"]=>
  object(stdClass)#33 (1) {
    ["_"]=>
    string(13) "USB80AUC085A0"
  }
  ["id"]=>
  int(292015)
  ["modelYear"]=>
  int(2008)
  ["name"]=>
  string(17) "5dr Avant Wgn Man"
  ["nameWoTrim"]=>
  string(17) "5dr Avant Wgn Man"
  ["mfrModelCode"]=>
  string(6) "8ED549"
  ["fleetOnly"]=>
  bool(false)
  ["modelFleet"]=>
  bool(false)
  ["passDoors"]=>
  int(4)
  ["altBodyType"]=>
  string(13) "Station Wagon"
  ["drivetrain"]=>
  string(15) "All Wheel Drive"
}

So far I have been successful in creating and calling the variables stored as objects.

Here is what I have done so far.

Create the variables based on the output:

$manuf = $result->vinDescription->WorldManufacturerIdentifier;
$vin = $result->vinDescription->vin;
$year = $result->vinDescription->modelYear;
$make = $result->vinDescription->division;
$model = $result->vinDescription->modelName;
$style = $result->vinDescription->styleName;
$body= $result->vinDescription->bodyType;

Print the variables to describe the vehicle:

echo "Manufacturer: ".$manuf;
echo "<br>";
echo "Year: ".$year;
echo "<br>";
echo "Make: ".$make;
echo "<br>";
echo "Model: ".$model;
echo "<br>";
echo "Trim: ".$style;
echo "<br>";
echo "Body style: ".$body;

The output:

Manufacturer: Germany Audi Nsu 
Year: 2008
Make: Audi
Model: S4
Trim: 5dr Avant Wgn
Body style: Wagon 4 Dr.
Drive type: Small Wagon

I am having trouble creating and calling certain variables that seem to be nested in arrays. The two variables I am interested in creating are Number of Doors and Drivetrain. They are in the bottom of the original output named passDoors and drivetrain.

I have tried to create the variable like this: $drivetype = $result['style']['drivetrain'];

However, I been unsuccessful. Any ideas as to how I can create and call passDoors and drivetrain from the original output? Any feedback is appreciated.


Encounter another roadblock. It seems the further I dig into the data the more complex calling data to create new variables. Here is the section I am struggling with:

["technicalSpecification"]=>
 array(97) {
[0]=>
object(stdClass)#640 (2) {
  ["titleId"]=>
  int(1)
  ["value"]=>
  array(2) {
    [0]=>
    object(stdClass)#641 (3) {
      ["styleId"]=>
      array(2) {
        [0]=>
        int(292015)
        [1]=>
        int(292016)
      }
      ["value"]=>
      string(7) "Audi S4"
      ["condition"]=>
      string(3) "-PT"
    }
    [1]=>
    object(stdClass)#642 (3) {
      ["styleId"]=>
      array(2) {
        [0]=>
        int(292015)
        [1]=>
        int(292016)
      }
      ["value"]=>
      string(7) "Audi S4"
      ["condition"]=>
      string(0) ""
    }
  }
}
[1]=>
object(stdClass)#643 (2) {
  ["titleId"]=>
  int(2)
  ["value"]=>
  object(stdClass)#644 (3) {
    ["styleId"]=>
    array(2) {
      [0]=>
      int(292015)
      [1]=>
      int(292016)
    }
    ["value"]=>
    string(12) "5 Door Wagon"
    ["condition"]=>
    string(0) ""
  }
}
[2]=>
object(stdClass)#645 (2) {
  ["titleId"]=>
  int(6)
  ["value"]=>
  object(stdClass)#646 (3) {
    ["styleId"]=>
    array(2) {
      [0]=>
      int(292015)
      [1]=>
      int(292016)
    }
    ["value"]=>
    string(15) "All-Wheel Drive"
    ["condition"]=>
    string(0) ""
  }
}

I am attempting to extract "Audi S4", "5 Door Wagon", and "All-Wheel Drive"

Thanks so much for any input.

mugelloblue
  • 137
  • 1
  • 11
  • Check out [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – MonkeyZeus Sep 14 '18 at 19:45
  • It's an object, so you use `->`, not `[]`. – Barmar Sep 14 '18 at 19:57
  • 1
    is it correct that the var_dump output you posted is wrong? There is an `}` after `WorldManufacturerIdentifier` but I think that is not true! Is it a typo? And the style-array is incomplete. – steven Sep 14 '18 at 20:07

1 Answers1

1

While style is an array you should handle it as an array. It seems to be the case that there could be passed multiple styles. So there isn't the one drivetrain but multiple drivetrains.

You may do it like this:

    foreach($result->style as $style) {

        echo $style->passDoors . "\n";
        echo $style->drivetrain . "\n";
    }

If you are sure that there is always only one style with index 0 like in the given example you may alternatively be able to access it like this:

echo $result->style[0]->passDoors . "\n";
echo $result->style[0]->drivetrain . "\n";
steven
  • 4,868
  • 2
  • 28
  • 58
  • Appreciate your response! You really helped me out. I encountered another roadblock when I dug further into the data. I just edited the question and presented the block of data I am trying to extract from at the end. Looking to create three more variables. Any ideas? Thanks again. – mugelloblue Sep 20 '18 at 01:34