code_legend
code_legend

Reputation: 3285

Grab JSON value using PHP

Below is the result of the code written below, where I would like to grab the first id element, and equal that to a php variable such as $customerID = whats in the green enter image description here

Below is how this json data is generated:

try {
 $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $amount,
      'currency' => 'cad',
            'capture' => 'false',

      'description'=>  $courseTitle

  ));

 $charge_json =  $charge->__toJSON();
 $charge_json->_values['id'];

echo "<pre>";
var_dump($charge_json);
echo "</pre>";

} catch(\Stripe\Error\Card $e) {
  // The card has been declined
  $stripChargeValid = false;
  echo "failed";

}

Note:

The notice of error found on line 58 is the below line:

 $charge_json->_values['id'];

The reason I did the following line was because at its core the json data being populated was protected and hence i would not be able to grab the id:

 $charge_json =  $charge->__toJSON();

Upvotes: 0

Views: 71

Answers (2)

user1893702
user1893702

Reputation:

You need to json_decode() the $customer data (the json image you posted). Afterwards you can access the id by $customer->id. Call print_r() on the json_decoded $customer to see its structure.

Upvotes: 2

SysVoid
SysVoid

Reputation: 584

You will want to use json_decode().

I've made you an example. PHPaste Snippet.

Upvotes: 1

Related Questions