1

Please help me create json from this code , example

{
    "results": [
        {
            "DASH": "fff72f33-e946-5945-b198-b405a652e078",
            "DOGE": "600ea0a0-dff7-5bf5-8eb2-0b56c3ba5922",
            }]}

Here is the php code I want to create this type of json response from it

<?php 
$curl = curl_init(); 
 
curl_setopt_array($curl, array( 
    CURLOPT_URL => "https://api.jobians.top/coinbase/accounts.php?api=N7696pIFgkF7vagl&key=AF3RO2AIrhuLpw40kyEUnZcc975dRfQq", 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_ENCODING => "", 
    CURLOPT_MAXREDIRS => 2, 
    CURLOPT_TIMEOUT => 10, 
    CURLOPT_FOLLOWLOCATION => true, 
    CURLOPT_CUSTOMREQUEST => "GET" 
    )); 
$response = curl_exec($curl); 
 
curl_close($curl); 

header('Content-Type: application/json');
$json2 = json_decode($response, true);

$id_array = array_map(function($el) { return $el['id']; }, $json2['data']);
foreach ($json2['data'] as $el) {

echo "{$el['balance']['currency']}: {$el['id']}\n";

 }
?>

Please help me create json from this code

Please help me create json from this code

Ok ok
  • 19
  • 5
  • you need to create an array first from your response data inside the foreach loop and then when the array is filled with all the values you need you can use json_encode to create the output – newbiedev Aug 02 '22 at 19:27
  • Please how can I do it – Ok ok Aug 02 '22 at 19:28

1 Answers1

2

It may be easier to add these lines to an object/array as data, then print the data as a json encoded string.

$outputObject = [];
foreach ($json2['data'] as $el) {
    $outputObject[$el['id']] = $el['balance']['currency'];
}
echo json_encode( $outputObject );

also, the output you are printing seems to be invalid JSON. you didn't give a great example of the output structure you expect, so I took liberties and guessed.

dqhendricks
  • 19,030
  • 11
  • 50
  • 83