从KashFlow API获取所有客户记录时出现问题

时间:2015-03-07 14:23:20

标签: php arrays

我已经完成了大部分的腿部工作。我已经连接到API并且它返回了一系列客户对象,我正在努力解决如何遍历每个客户对象。

以下是API调用返回的结构:

stdClass Object
(
[Customer] => Array
    (
        [0] => stdClass Object
            (
                [CustomerID] => 20409125
                [Name] => Computer Says No
                [Telephone] => 
                [Mobile] => 
                [Email] => myemail@address.com
                [Website] => 
            )
        [1] => stdClass Object
            (
                [CustomerID] => 20409126
                [Name] => Joe Bloggs
                [Telephone] => 
                [Mobile] => 
                [Email] => myemail@address.com
                [Website] => 
            )

        [3] => stdClass Object
            (
                [CustomerID] => 20409127
                [Name] => Jane Bloggs
                [Telephone] => 
                [Mobile] => 
                [Email] => myemail@address.com
                [Website] => 
            )
    )
)

以下是我试图循环回复的方法

$kashflow = new Kashflow('my username','my password');
$customers = $kashflow->getCustomers();

foreach($customers as $customer){
echo "<pre>";
print_r($customer->CustomerID);
echo "</pre>";
}

如果我print_r($customers)我得到了:

stdClass Object
(
[GetCustomersResult] => stdClass Object
    (
        [Customer] => Array
            (
                [0] => stdClass Object
                    (
                        [CustomerID] => 20409125
                    )
                [1] => stdClass Object
                    (
                        [CustomerID] => 20409126
                    )
                [2] => stdClass Object
                    (
                        [CustomerID] => 20409127
                    )
              )
      )
)

2 个答案:

答案 0 :(得分:0)

试试这个:

$kashflow = new Kashflow('my username','my password');
$customers = $kashflow->getCustomers();

foreach($customers->GetCustomersResult->Customer as $customer){
    echo "<pre>";
    print_r($customer->CustomerID);
    echo "</pre>";
}

答案 1 :(得分:0)

你正在获得像

这样的记录
    $first_id= array_column($customers, 'cust_id');
    foreach($first_id as $id)
    {
      echo "<pre>";
      echo $id;
      echo "</pre>"
    }