douchun1900 2010-01-24 22:47
浏览 34
已采纳

从多维数组中获取值的问题

The following code:

      $options = $value[$i]['options'];
      print_r($options);

outputs the following result:

Array ( 
  [0] => stdClass Object ( 
        [id] => 1 
        [field_id] => 1 
        [option_name] => I'm a normal user ) 
  [1] => stdClass Object ( 
        [id] => 2 
        [field_id] => 1 
        [option_name] => Store owner ) 
  [2] => stdClass Object ( 
        [id] => 3 
        [field_id] => 1 
        [option_name] => Brand owner ) 
  [3] => stdClass Object ( 
        [id] => 4 
        [field_id] => 1 
        [option_name] => Designer ) 
)

So why can't I output "I'm a normal user" using echo $options[0]["option_name"] ?

My plan is to output id and option_name using a foreach loop:

  foreach ($options as $option)
  {
    echo "<option value='".$option["id"]."'>".$option["option_name"]."</option>";
  } 

This should be easy.... but I'm fumbling :(

  • 写回答

3条回答 默认 最新

  • douxing1969 2010-01-24 22:50
    关注

    The second level is not an array but an object. This would be correct:

    $options[0]->option_name
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?