'Populate Dropdown from Zoho Response

I am trying to populate a dropdown from the ZOHO API response I am getting with my request. I have two files. request.php and zoho.php.

I am receiving the response from request.php as below.

object(stdClass)#2 (4) {
  ["code"]=>
  int(0)
  ["message"]=>
  string(7) "success"
  ["invoices"]=>
  array(200) {
    [0]=>
    object(stdClass)#3 (54) {
      ["invoice_id"]=>
      string(19) "2163791000003899301"
    }
  }
}

in order to get the result I am decoding the object as below

$result = curl_exec($ch);
curl_close($ch);
$decode_data = json_decode($result);
var_dump($decode_data);

I am having a popup in zoho.php.

<select value="Select Zoho Invoice" name="zohoinvoice" id="zohoinvoice" class="SlectBox form-control">
   <?php echo zohoFunc(); ?>
</select>


function zohoFunc(){
   global $decode_data;
   $output='';
   $output .= '<option value = "Select Invoice">Select Invoice</option>';
   foreach($decode_data as $inv){
     $output[] .= '<option value = "'.$inv[0]->invoice_id.'">'.$inv[0]->invoice_number.'</option>';
   }
   return $output;
}

The way I am looping the data I have received is below. I am trying to get as the way how we populate a dropdown from SQL which is a string and zoho's case its an array. Where I am making the mistake?



Solution 1:[1]

You you are looking for all the invoices in your data set. So try.

foreach($decode_data->invoices  as $myCurrentInvoice) {
    echo $myCurrentInvoice->invoice_id;
    echo '<br>';
    //$output[] .= '<option value = "'.$myCurrentInvoice->invoice_id.'">'.$myCurrentInvoice->invoice_number.'</option>';
    
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nicholas Stom