2

I have following array of stdClass Object structure and want count number of offers. Std class is received as a response from third party API so it is dynamic.

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***
                    )

            )

    )

i want count number of arrays in [Offer], for that i have done following:

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

but it gives 1 as a count like count----------1 in this case count is 3 and i want 3 as output. Please suggests. i have also used this echo "count----------".count((array)$offers); This also dont works.

Vishal Patel
  • 1,715
  • 16
  • 26

4 Answers4

1

You can do it like convert "stdClass Object" into normal array and try after that count($offer);

Just write (array)$object; It will convert as normal array

Lalji Nakum
  • 380
  • 1
  • 14
0

Count should work.

<?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

Pupil
  • 23,834
  • 6
  • 44
  • 66
0

I have solved my question by following way:

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

this loops itreate only once, and give me result.

Vishal Patel
  • 1,715
  • 16
  • 26
0

Try this :

<?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());
?>

The above example will output:

int(1)
int(3)

Answer Source

Kaleem Ullah
  • 6,799
  • 3
  • 42
  • 47