1

How to get the values from array? I am stucked in this. Anybody knows please help me. I am getting only main array's values.

[0] => stdClass Object
            (
                [id] => 68427249
                [active] => 1
                [name] => Three Way
                [status] => open
                [market_type_id] => 3896
                [market_type_name] => Three Way
                [market_type_order_number] => 1000
                [event_id] => 8669447
                [event_name] => Nuovo Campobasso Calcio vs Jesina Calcio
                [line] => 
                [scope] => full_event
                [order_number] => 0
                [selections] => Array
                    (
                        [0] => stdClass Object
                            (
                                [id] => 558087597
                                [name] => Nuovo Campobasso Calcio
                                [status] => ok
                                [odd] => 2.32
                                [lay_odd] => 1.0
                                [order_number] => 0
                                [line] => 
                                [market_subtype_id] => 293985
                                [market_subtype_code] => 11
                                [market_subtype_name] => Home
                                [market_subtype_order_number] => 0
                            )

                        [1] => stdClass Object
                            (
                                [id] => 558087568
                                [name] => Draw
                                [status] => ok
                                [odd] => 3.13
                                [lay_odd] => 1.0
                                [order_number] => 1
                                [line] => 
                                [market_subtype_id] => 293986
                                [market_subtype_code] => 10
                                [market_subtype_name] => Draw
                                [market_subtype_order_number] => 1
                            )

                        [2] => stdClass Object
                            (
                                [id] => 558087589
                                [name] => Jesina Calcio
                                [status] => ok
                                [odd] => 2.57
                                [lay_odd] => 1.0
                                [order_number] => 2
                                [line] => 
                                [market_subtype_id] => 293987
                                [market_subtype_code] => 12
                                [market_subtype_name] => Away
                                [market_subtype_order_number] => 2
                            )

                    )

            )

my code

foreach($m as $ms)
    {

    echo '<tr><td>'.$ps->id.'</td><td>'.$ms->event_name.'</td><td>'.$ms['selections']->odd.'</td></tr>';
    }

My code is not working to get values from [selections]. Please help me to get values.

3 Answers 3

1

It will be something like this to access the data of selections as its also array. So, you need to run loop for selections to get its value.

foreach ($array as $obj) {
   echo $obj->name;
   echo $obj->status;
   if (is_array($obj->selections)) {
          foreach ($obj->selections as $selection) {
                echo $selection->name;
                echo $selection->odd;
          }
   }
}

I hope it will help you.

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

Comments

0

You can access your object array values like this :

foreach ($objArr as $obj) {
   echo '<tr><td>'.$obj->id.'</td><td>'.$obj->event_name.'</td>';
   foreach ($obj->selections as $objS) {
      echo '<td>'.$objS->odd.'</td>';
   }
   echo '</tr>';
}

Comments

0
<?php

$user = (Object)["name" => "Meraj","email"=> "[email protected]", "education" => (Object)[
"school" => "JMI", "college" => "GGSIPU"]];
function printer($nested_object)
{
    foreach ($nested_object as $property => $value) {
        if (is_object($value)) {
            printer($value);
        } else {
            echo $property."=".$value."\n";
        }
    }
}
printer($user);

Instead of echoing you can do as per your requirements;

//output
name=Meraj
[email protected]
school=JMI
college=GGSIPU

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.