2015-11-05 65 views
2

我有stdClass Object結構的下面的數組,並且需要計數提供的數量。 Std類作爲第三方API的響應接收,因此它是動態的。stdClass對象的數量在php中的數組

stdClass Object 
    (
     [Offer] => Array 
      (
       [0] => stdClass Object 
        (
         [Offerid] => 1 
         [LoanAmount] => 2**** 
         [InterestRate] => 2* 
         [Term] => 36 
         [MonthlyPayment] => 7*** 
         [Annualfee] => 0 
         [OriginationFee] => 1*** 

        ) 

       [1] => stdClass Object 
        (
         [Offerid] => 1 
         [LoanAmount] => 2**** 
         [InterestRate] => 2* 
         [Term] => 36 
         [MonthlyPayment] => 7*** 
         [Annualfee] => 0 
         [OriginationFee] => 1*** 
        ) 

       [2] => stdClass Object 
        (
         [Offerid] => 1 
         [LoanAmount] => 2**** 
         [InterestRate] => 2* 
         [Term] => 36 
         [MonthlyPayment] => 7*** 
         [Annualfee] => 0 
         [OriginationFee] => 1*** 
        ) 

      ) 

    ) 

我想在計算陣列號[報價],爲我做了以下內容:

echo "count----------".count($offers); 

但它給像 count----------1 在這種情況下,計計數是和我想3作爲輸出。 請建議。 我也用過這個 echo「count ----------」。count((array)$ offers); 這也不工作。

+0

TRY - '計數($ your_object->優惠)'。 –

+0

我已經試過這個,它也給出了1. –

+0

你可以在https://eval.in/上做一個小提琴嗎? –

回答

0

我已經通過以下方式解決我的問題:

foreach ($offers as $key=> $value) 
{ 
    echo "<br/>count->".count($value); 
} 

這個循環itreate只有一次,給我造成的。

0

計數應該工作。

<?php 
$obj = new stdClass(); 
$obj->Offer = array(
    array('Offerid' => 1, 'LoanAmount' => '2***', 'InterestRate' => '2*', 'Term' => 36, 'MonthlyPayment' => '7***', 'Annualfee' => 0, 'OriginationFee' => '1***'), 
    array('Offerid' => 1, 'LoanAmount' => '2***', 'InterestRate' => '2*', 'Term' => 36, 'MonthlyPayment' => '7***', 'Annualfee' => 0, 'OriginationFee' => '1***'), 
    array('Offerid' => 1, 'LoanAmount' => '2***', 'InterestRate' => '2*', 'Term' => 36, 'MonthlyPayment' => '7***', 'Annualfee' => 0, 'OriginationFee' => '1***'), 
); 
echo count($obj->Offer); // Outputs 3 
?> 

Live example

+0

謝謝你的回答,但我不能這樣做。因爲上面的回覆是dyanmic,它是由APi發送的。 –

+0

你不能做什麼? '的var_dump(isset($對象 - >提供)); // true'和'var_dump(isset($ object-> Offera)); // false' –

1

你可以不喜歡它轉換 「stdClass的對象」 分爲正常陣列,之後計數($報價)嘗試;

只需寫入(array)$ object;它會轉換爲普通陣列

+0

這是一條評論,而不是一個答案。請提供代碼 –

+0

。如果有建議,請使用評論。 –

+0

@ Niranjan N Raju查看我的更新回答 –

0

嘗試這種情況:

<?php 
class Example { 
    public $public = 'prop:public'; 
    private $prv = 'prop:private'; 
    protected $prt = 'prop:protected'; 
} 

$arrayobj = new ArrayObject(new Example()); 
var_dump($arrayobj->count()); 

$arrayobj = new ArrayObject(array('first','second','third')); 
var_dump($arrayobj->count()); 
?> 

上例將輸出:

int(1) 
int(3) 

Answer Source