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.

6
  • Try - count($your_object->Offer). Commented Nov 5, 2015 at 6:22
  • i have already tried this, it also gives 1. Commented Nov 5, 2015 at 6:24
  • Can you make a fiddle on eval.in?? Commented Nov 5, 2015 at 6:27
  • 1
    Can you show more of your work. How is $offers defined. Commented Nov 5, 2015 at 6:31
  • Possible duplicate of PHP: Count an stdClass object Commented Nov 5, 2015 at 13:34

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

That's a comment and not an answer.
please provide code also. If its a suggestion, please use comments.
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

2 Comments

thanks for answer,but i cant do like this.because above response is dyanmic and it was send by APi.
What you can't do? var_dump(isset($object->Offer)); //true and var_dump(isset($object->Offera)); // false
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.