php在数组中读取数组的对象

时间:2013-06-23 10:05:51

标签: php arrays

 $values = (object)$arr;
 var_dump($values);

var_dump产生这些:

object(stdClass)[1]
  public 'time' => float 0.002
  public 'distance' => float 0.156
  public 'code' => string '1' (length=1)
  public 'result' => 
    array (size=17)
      0 => string '2.94053, 101.787, A' (length=24)
      1 => string '2.94043, 101.787, A' (length=24)
      2 => string '2.9404, 101.787, A' (length=23)
      3 => string '2.94029, 101.787, A' (length=24)
      4 => string '2.94025, 101.787, A' (length=24)
      5 => string '2.9402, 101.787, A' (length=23)
      6 => string '2.94016, 101.787, A' (length=24)
      7 => string '2.94007, 101.787, A' (length=24)
  public 'arrayPosition' => 
    array (size=1)
      0 => 
        array (size=1720)
          0 => string '2.93955, 101.788, B' (length=22)
          1 => string '2.93951, 101.788, B' (length=22)
          2 => string '2.93926, 101.788, B' (length=22)
          3 => string '2.93921, 101.788, B' (length=22)
          4 => string '2.9392, 101.788, B' (length=21)
          5 => string '2.93911, 101.788, B' (length=22)
          6 => string '2.93906, 101.789, B' (length=22)
          7 => string '2.93896, 101.789, B' (length=22)

如何阅读resultarrayPosition的每个值? 通过使用此

           echo "<br>".$values->time."<br>";
           echo $values->distance."<br>";
           echo $values->code."<br>";
           echo $values->result."<br>";
           echo $values->arrayPosition."<br>";

将在resultarrayPosition

上失败
Notice: Array to string conversion

3 个答案:

答案 0 :(得分:1)

访问arrayPosition如果您知道要通过

访问的元素的子索引,有几种方法可以访问
 $values->arrayPosition[0][index_number]; //you may require the first dimension index if you have more than one element there as well

另一种方法是使用循环

 foreach ($values->arrayPosition as $levelone) {
      foreach ($levelone as $key => $leveltwo) {
            echo $leveltwo;
      }
 }

答案 1 :(得分:1)

foreach($values as $value)
{
  foreach($value->result as $result1)
  {
    echo($result1);
    echo("<br>");
  }
  foreach($value->arrayPosition as $arrayposition2)
  {
    foreach($arrayposition2 as $arrayposition_child)
    {
      echo($arrayposition_child);
      echo("<br>");
    }
  }
}

答案 2 :(得分:1)

要访问这两个变量,您可以:

1)
foreach( $values->result as $row ) {
     echo $row;
}

2)
foreach( $values->arrayPosition as $mulRow ) {
     foreach( $mulRow as $row ) {
         echo $row;
     }
}