简体   繁体   中英

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 在此输入图像描述

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();

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.

You will want to use json_decode() .

I've made you an example. PHPaste Snippet .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM