0

I have this structure coming from an array dump:

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

How to parse it with PHP? In particular, I would like to assign values to normal PHP variables in order to print them or to put into an HTML table.

2
  • Can you just do print_r? Commented Sep 26, 2013 at 7:57
  • 1
    Why go through all the overhead of assigning values from this array of objects to variables; just access them as they are when you need them Commented Sep 26, 2013 at 8:00

2 Answers 2

1

You can access this structure like this

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

This will result

12:30:00
12:45:00
5

Why do you need variables for this? That will result in too many vars. Instead use a loop to iterate over the object and access the inner objects using "->"

Sign up to request clarification or add additional context in comments.

1 Comment

Great,Jabran!!! :-) I define $object by passing to this variable the object coming from the array (which is actually a JSONed array). So I've solved this way: $object = json_decode($response->result); $response->result comes from a CURL call, which queries a web service and get 'result' in return. Again, thank you!!! :-)
1

You need to make use of the extract() on 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";
?>

OUTPUT :blue, large, sphere, medium

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.