1

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"
                }
            }
}
IMSoP
  • 65,743
  • 7
  • 83
  • 127
the5thace
  • 51
  • 2
  • 8
  • 3
    If you view it on a web page, it naturally gets squeezed and hard to read. Use or print it on the console to see the nested structure. – mario Jul 27 '13 at 21:35
  • The primary purpose behind `var_dump()` is to debug, you get a printed output of the value of the variable you are dumping to string (includes types, length, and values). You shouldn't be "reading" a `var_dump()`. You should be "reading" the variable. – BLaZuRE Jul 27 '13 at 21:39
  • 1
    I've edited the question to include the indentation that would show if you viewed source (thanks to @Herbert from whose answer I copied the formatting). – IMSoP Jul 27 '13 at 21:46
  • 1
    Thanks @IMSoP. It was painstaking. Glad someone appreciated it. :) – Herbert Jul 27 '13 at 21:59

2 Answers2

4

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";
}
Community
  • 1
  • 1
Herbert
  • 5,302
  • 2
  • 20
  • 34
1

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 ...
Tobias Golbs
  • 4,450
  • 3
  • 25
  • 49