0

I have a json array for data in php and I am trying to echo the value "transaction id". Can you please help

SquareConnect\Model\ChargeResponse Object
(
    [errors:protected] => 
    [transaction:protected] => SquareConnect\Model\Transaction Object
        (
            [id:protected] => cx4V
            [location_id:protected] => 5TRMFA
            [created_at:protected] => 2017-12-11T01:53:22Z
            [tenders:protected] => Array
                (
                    [0] => SquareConnect\Model\Tender Object
                        (
                            [id:protected] => 001F
                            [location_id:protected] => 5TRA
                            [transaction_id:protected] => cx4x35peV
                            [created_at:protected] => 2017-12-11T01:53:22Z
                            [note:protected] => Online Transaction
                            [amount_money:protected] => SquareConnect\Model\Money Object
                                (
                                    [amount:protected] => 100
                                    [currency:protected] => USD
                                )
aaa
  • 857
  • 4
  • 25
  • 46
  • First print your Json array properly like echo "
    "; print_r($json); die;
    – Amit Gupta Dec 11 '17 at 02:45
  • Possible duplicate of [Returning JSON from a PHP Script](https://stackoverflow.com/questions/4064444/returning-json-from-a-php-script) – catzilla Dec 11 '17 at 02:51
  • Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – mickmackusa Dec 11 '17 at 07:32

2 Answers2

1

I cab give you technique. Please code yourself. 1. Convert JSON to ARRAY. 2. Then select value from array .

$json = '
{
    "type": "donut",
    "name": "Cake"
}';

$yummy = json_decode($json);

echo $yummy->type; //donut
0

You can first convert your Json data to Array like below:

$json = json_decode( json_encode($json), true);

Then In foreach you can get your data very easily:

foreach($json as $data){
   echo $data['transaction_id'];
}

If you want to simply print your Json data without converting to array then do like below:

foreach($json as $data){
   echo $data->transaction_id;
}

And before doing anything, print your json data so that it will be more clear where transaction_id is actually located and how to fetch it.

echo "<pre>"; print_r($json); die;
Amit Gupta
  • 2,771
  • 2
  • 17
  • 31