Reputation: 51
Here is a sample var_dump. Now, how do I know how to construct a foreach from it to load any particular word or fragment in to an array?
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
Upvotes: 1
Views: 4267
Reputation: 5778
Before we can decipher it, we have to format it.
Tobias Kun's answer shows a very good way to format the var_dump
output so you can read it.
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
You have a stdClass object with a property called ,"noun". noun is an abject with a property called "syn", which is an array of strings.
Suppose we call the object $object
. Then we can access the array like:
echo $object->noun->syn[23];
which gives us canine
. So a loop might look like:
foreach($data->noun->syn as $value) {
echo $value . "<br>\n";
}
Upvotes: 4
Reputation: 4616
First of all you should really increase the quality of your questions. The code is not formatted at all.
If you use echo "<pre>" . print_r($your_data_object_or_array,1) . "</pre>"
your data will be formatted fine.
If i understand you right this should help you:
foreach($data['noun']['syn'] as $value) {
//with this you loop through all your words in "syn" e.g. domestic, "Canis familiaris etc."
echo $value . "<br>";
}
//Ouput:
domestic
Canis familiaris
frump
cad
etc ...
Upvotes: 1