简体   繁体   English

检索发票数组而不是 object PHP

[英]Retrieve an array of invoices instead of an object PHP

I'm trying to retrieve a list of invoices per customer.我正在尝试检索每个客户的发票列表。 So far I'm only retrieving the first object of the list.到目前为止,我只检索列表的第一个 object。 But how do I retrieve the whole list?但是我如何检索整个列表? I'm a beginner in PHP so I really can't figure this out and the docs of Chargebee aren't clarifying anything for me.我是 PHP 的初学者,所以我真的想不通,Chargebee 的文档也没有为我澄清任何事情。

Here are the docs: https://apidocs.eu.chargebee.com/docs/api/invoices?prod_cat_ver=2&lang=php#list_invoices以下是文档: https://apidocs.eu.chargebee.com/docs/api/invoices?prod_cat_ver=2&lang=php#list_invoices

And this is what I tried so far:这是我到目前为止尝试过的:

$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);

Unfortunately I'm only retrieving the first item of the list.不幸的是,我只检索列表的第一项。 How do I get the whole list in an array, but encoded to a json string?如何获取数组中的整个列表,但编码为 json 字符串?

You keep overwriting your $invoice variable.您不断覆盖$invoice变量。

Creating and empty array and pushing every invoice onto that should yield the result you want.创建并清空数组并将每张发票推送到该数组上应该会产生您想要的结果。 So try this:所以试试这个:

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM