0

我有这个来自数组转储的结构:

0 => 
object(stdClass)[3]
  public 'start' => string '12:30:00' (length=8)
  public 'end' => string '12:45:00' (length=8)
  public 'structure' => 
    object(stdClass)[5]
      public 'structure_id' => int 5
      public 'structure_name' => string 'LABORATORY 1' (length=18)
  public 'specialist' => 
    object(stdClass)[6]
      public 'specialist_id' => int 222
      public 'specialist_surname' => string 'Smith' (length=7)
      public 'specialist_name' => string 'John' (length=9)
      public 'specialist_signature' => string 'Dr.' (length=3)
  public 'price' => float 80.5
1 => 
object(stdClass)[3]
  public 'start' => string '12:30:00' (length=8)
  public 'end' => string '12:45:00' (length=8)
  public 'structure' => 
    object(stdClass)[5]
      public 'structure_id' => int 5
      public 'structure_name' => string 'LABORATORY 4' (length=18)
  public 'specialist' => 
    object(stdClass)[6]
      public 'specialist_id' => int 222
      public 'specialist_surname' => string 'White' (length=7)
      public 'specialist_name' => string 'Jack' (length=9)
      public 'specialist_signature' => string 'Dr.' (length=3)
  public 'price' => float 80.5
2 => 
object(stdClass)[3]
  public 'start' => string '12:30:00' (length=8)
  public 'end' => string '12:45:00' (length=8)
  public 'structure' => 
    object(stdClass)[5]
      public 'structure_id' => int 5
      public 'structure_name' => string 'LABORATORY 9' (length=18)
  public 'specialist' => 
    object(stdClass)[6]
      public 'specialist_id' => int 222
      public 'specialist_surname' => string 'Brown' (length=7)
      public 'specialist_name' => string 'Lester' (length=9)
      public 'specialist_signature' => string 'Dr.' (length=3)
  public 'price' => float 80.5

如何用 PHP 解析它?特别是,我想为普通的 PHP 变量赋值,以便打印它们或放入 HTML 表中。

4

2 回答 2

1

您需要使用extract()PHP 上的

<?php
$size = "large";
$var_array = array("color" => "blue",
                   "size"  => "medium",
                   "shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size\n";
?>

输出:蓝色,大,球,中

于 2013-09-26T07:58:07.927 回答
1

你可以像这样访问这个结构

echo $object[0]->start;
echo $object[0]->end;
echo $object[0]->structure->specialist_id;

这将导致

12:30:00
12:45:00
5

为什么你需要变量呢?这将导致太多的变量。而是使用循环遍历对象并使用“->”访问内部对象

于 2013-09-26T08:05:06.900 回答