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.
-
Can you just do print_r?user796443– user7964432013年09月26日 07:57:59 +00:00Commented Sep 26, 2013 at 7:57
-
1Why go through all the overhead of assigning values from this array of objects to variables; just access them as they are when you need themMark Baker– Mark Baker2013年09月26日 08:00:48 +00:00Commented Sep 26, 2013 at 8:00
2 Answers 2
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 "->"
answered Sep 26, 2013 at 8:05
-
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!!! :-)DeepVoid– DeepVoid2013年09月26日 08:18:08 +00:00Commented Sep 26, 2013 at 8:18
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
answered Sep 26, 2013 at 7:58
Shankar Narayana Damodaran Shankar Narayana Damodaran
68.6k43 gold badges101 silver badges129 bronze badges
lang-php