TrialByFire Posted June 6 Share Posted June 6 (edited) Hey PHP Homies! I'm trying to make this JSON into PHP so I can send it to an API using curl. I have no problem with the first part of it, but I'm getting totally zonked on the cart contents where the items are in their own object. { "store_id":"test", "api_token":"test", "checkout_id":"test", "txn_total":"4.00", "environment":"qa", "action":"preload", "cart":{ "items":[ { "description":"item 1", "unit_cost":"1.00", "quantity":"3" }, { "description":"item 2", "unit_cost":"1.00", "quantity":"1" } ], "subtotal":"4.00" } } What is the best way to represent something like this in PHP and then convert it to JSON format to send out to CURL? Thanks in advance! Edited June 6 by TrialByFire Quote Link to comment https://forums.phpfreaks.com/topic/328340-how-to-represent-this-json-data-as-php/ Share on other sites More sharing options...
Barand Posted June 6 Share Posted June 6 JSON is a data structure converted to string format to make storage and transfer easier. Define your json as a a PHP string ... $json_str = ' { "store_id":"test", "api_token":"test", "checkout_id":"test", "txn_total":"4.00", "environment":"qa", "action":"preload", "cart":{ "items":[ { "description":"item 1", "unit_cost":"1.00", "quantity":"3" }, { "description":"item 2", "unit_cost":"1.00", "quantity":"1" } ], "subtotal":"4.00" } } '; Provided it is the correct format you use this in the api call. To use the data in PHP you need to decode it with json_decode()... $data = json_decode($json_str, true); # the second parameter "true" converts it to an array. # without it you get a php object. // view the resulting array data echo '<pre>' . print_r($data, 1) . '</pre>'; ... which gives us ... Array ( [store_id] => test [api_token] => test [checkout_id] => test [txn_total] => 4.00 [environment] => qa [action] => preload [cart] => Array ( [items] => Array ( [0] => Array ( [description] => item 1 [unit_cost] => 1.00 [quantity] => 3 ) [1] => Array ( [description] => item 2 [unit_cost] => 1.00 [quantity] => 1 ) ) [subtotal] => 4.00 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/328340-how-to-represent-this-json-data-as-php/#findComment-1654625 Share on other sites More sharing options...
gizmola Posted June 7 Share Posted June 7 @Barand gave you code and instructions for taking json and converting it into php. From what I read, you actually want to go the opposite direction -- take a php object or array (or some combination) and convert that into json. You do that using json_encode(). There are flags you might need that control how certain PHP types get converted to match what the api requires. Quote Link to comment https://forums.phpfreaks.com/topic/328340-how-to-represent-this-json-data-as-php/#findComment-1654679 Share on other sites More sharing options...
TrialByFire Posted June 11 Author Share Posted June 11 Thanks for the reply everyone, On 6/7/2025 at 6:20 PM, gizmola said: @Barand gave you code and instructions for taking json and converting it into php. From what I read, you actually want to go the opposite direction -- take a php object or array (or some combination) and convert that into json. You do that using json_encode(). There are flags you might need that control how certain PHP types get converted to match what the api requires. No basically I wanted to know how to actually declare the PHP object so json_encode() would return everything properly. What tripped me up was that there's an Array inside an Array inside of an Array and it made the actual declaration too confusing to write. So I broke it up into sub arrays and then declared the largest array by calling the smaller arrays. I have it working now, and I really appreciate the help! Quote Link to comment https://forums.phpfreaks.com/topic/328340-how-to-represent-this-json-data-as-php/#findComment-1655013 Share on other sites More sharing options...
Barand Posted June 12 Share Posted June 12 PHP array... $data = [ 'store_id' => 'test', 'api_token' => 'test', 'checkout_id' => 'test', 'txn_total' => '4.00', 'environment' => 'qa', 'action' => 'preload', 'cart' => [ 'items' => [ [ 'description' => 'Item 1', 'unit_cost' => '1.00', 'quantity' => '3' ], [ 'description' => 'Item 2', 'unit_cost' => '1.00', 'quantity' => '1' ] ], 'subtotal' => '4.00' ] ]; $json = json_encode($data); echo $json; gives this json string (spaces and newlines added for readability) { "store_id":"test", "api_token":"test", "checkout_id":"test", "txn_total":"4.00", "environment":"qa", "action":"preload", "cart":{ "items":[ {"description":"Item 1", "unit_cost":"1.00", "quantity":"3" }, { "description":"Item 2", "unit_cost":"1.00", "quantity":"1" } ], "subtotal":"4.00" } } Quote Link to comment https://forums.phpfreaks.com/topic/328340-how-to-represent-this-json-data-as-php/#findComment-1655060 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.