如何使用下面的数组从foreach循环获取数据?

时间:2017-02-08 05:57:13

标签: php arrays

如何从数组中获取值?我被困在这里。有人知道请帮帮我。我只获得主数组的值。

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

                    )

            )

我的代码

foreach($m as $ms)
    {

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

我的代码无法从[选择]中获取值。请帮助我获得价值。

3 个答案:

答案 0 :(得分:1)

这样可以将选择数据作为其数组来访问。因此,您需要为选择运行循环以获取其值。

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

我希望它会对你有所帮助。

答案 1 :(得分:0)

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

而不是回应你可以按照你的要求做;

//output
name=Meraj
email=merajsiddiqui@outlook.com
school=JMI
college=GGSIPU

答案 2 :(得分:0)

您可以像这样访问对象数组值:

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