简体   繁体   中英

How to get data from foreach loop using below Array?

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.

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.

<?php

$user = (Object)["name" => "Meraj","email"=> "merajsiddiqui@outlook.com", "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=merajsiddiqui@outlook.com
school=JMI
college=GGSIPU

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>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM