badweasel
badweasel

Reputation: 2399

how to determine if a json_decode contains a deep value in php

I've been searching and can't find an exact duplicate. Although there are many related questions on OS they don't tell me what I'm looking for exactly.

I'm a c coder not a php coder, so I'm just faking my way through as my php guy isn't available. I have server software that checks an in app purchase receipt from apple and does stuff with the results, including putting them in a table on my server database.

$endpoint="https://sandbox.itunes.apple.com/verifyReceipt";
$response=$objDB->verifyreceiptProcess($endpoint,$postData);
// parse the response data
$data1 = json_decode($response);  //  Response of sandbox

if( $data1->status == 0){  //  Sandbox Status =0
   $insertPurchase[0]["Field"]="user_id";
   $insertPurchase[0]["Value"]=$params['user_id'];
   $insertPurchase[1]["Field"]="product_id";
   $insertPurchase[1]["Value"]=$data1->receipt->product_id;
   $insertPurchase[2]["Field"]="receiptData";
   $insertPurchase[2]["Value"]=substr($params['receipt'],0,50);
   $insertPurchase[3]["Field"]="game_title";
   $insertPurchase[3]["Value"]=$params['game_id'];
   $insertPurchase[4]["Field"]="date_of_purchasing";
   $insertPurchase[4]["Value"]=$data1->receipt->purchase_date;
   $insertPurchase[5]["Field"]="apple_transaction_id";
   $insertPurchase[5]["Value"]=$data1->receipt->transaction_id; 
   $insertPurchase[6]["Field"]="app_item_id";
   $insertPurchase[6]["Value"]=$data1->receipt->app_item_id;
   ....

Then later it query_inserts this into my table to record the purchase, and returns info back to my app.

The problem is that right now the field app_item_id isn't there. My guess is that until the app is submitted this field isn't returned. This causes my server software to just return "false". If I call the api from safari, the word "false" prints instead of the formatted json result.

So I need to error check this last line up there to make sure that field is there before sticking it in $insertPurchase[6]["Value"] and if not put "" in there.

I assume isset can be used but don't understand how to use it to detect something this deep in a json result.

Sorry if this question is too basic.

Upvotes: 0

Views: 139

Answers (1)

user1470118
user1470118

Reputation: 412

This should work:

if(isset($data1->receipt->app_item_id))
{
    $insertPurchase[6]["Value"]=$data1->receipt->app_item_id;
} else {
     $insertPurchase[6]["Value"]="";
}

Upvotes: 1

Related Questions