检索发票数组而不是对象PHP

tf7tbtn2  于 2023-05-21  发布在  PHP
关注(0)|答案(2)|浏览(101)

我正在检索每个客户的发票列表。到目前为止,我只检索了列表的第一个对象。但我如何检索整个列表?我是PHP的初学者,所以我真的不能弄清楚这一点,Chargebee的文档也没有为我澄清任何事情。
以下是文档:https://apidocs.eu.chargebee.com/docs/api/invoices?prod_cat_ver=2&lang=php#list_invoices
这就是我到目前为止所尝试的:

$customerID = $_POST['customerID']; 

require_once(dirname(__FILE__) . '/chargebee-php-2.8.3/lib/ChargeBee.php');

ChargeBee_Environment::configure("xxxx","xxxx");

$result = ChargeBee_Invoice::all(array(
  "limit" => 13,
  "sortBy[desc]" => "date",
  "customerId[is]" => (string)$customerID
  ));
  
foreach($result as $entry){
  $invoice = $entry->invoice();
}

$array = (array) $invoice;

echo json_encode($array);

不幸的是,我只检索列表的第一项。我如何在数组中获得整个列表,但编码为JSON字符串?

lrpiutwd

lrpiutwd1#

你不断覆盖你的$invoice变量。
创建一个空数组,并将每个发票推到该数组上,应该会产生您想要的结果。试试这个:

$customerID = $_POST['customerID']; 

require_once(dirname(__FILE__) . '/chargebee-php-2.8.3/lib/ChargeBee.php');

ChargeBee_Environment::configure("xxxx","xxxx");

$result = ChargeBee_Invoice::all(array(
  "limit" => 13,
  "sortBy[desc]" => "date",
  "customerId[is]" => (string)$customerID
  ));

$invoicesArr = [] // create empty array
  
foreach($result as $entry){
  array_push($invoicesArr, $entry->invoice()); // foreach invoice, push it to array
}

echo json_encode($invoicesArr);
yzuktlbb

yzuktlbb2#

你可以使用chargebee给出的getValues()函数。下面是一个例子。

$result = ChargeBee_Invoice::all(array(
 "limit" => 13,
 "sortBy[desc]" => "date",
 "customerId[is]" => (string)$customerID
));

if (count($result) > 0) {

   $invoices = [];

   foreach ($result as $value) {

       $invoices[] = $value->invoice()->getValues();

   }

   echo json_encode($invoices);

} else {

   echo "Invoices not found";

}

相关问题