Jump to content

How to load this var_dump data in to a variable with a foreach?


the5thace

Recommended Posts

This is the var_dump of a json_decode function. How do I parse it with a foreach and load it in to an array?

 

object(stdClass)#1 (2) { ["noun"]=> object(stdClass)#2 (1) { ["syn"]=> array(39) { [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" [25]=> string(4) "chap" [26]=> string(4) "cuss" [27]=> string(18) "disagreeable woman" [28]=> string(5) "fella" [29]=> string(6) "feller" [30]=> string(6) "fellow" [31]=> string(4) "gent" [32]=> string(3) "lad" [33]=> string(7) "sausage" [34]=> string(9) "scoundrel" [35]=> string(4) "stop" [36]=> string(7) "support" [37]=> string(16) "unpleasant woman" [38]=> string(7) "villain" } } ["verb"]=> object(stdClass)#3 (2) { ["syn"]=> array(10) { [0]=> string(5) "chase" [1]=> string(11) "chase after" [2]=> string(5) "trail" [3]=> string(4) "tail" [4]=> string(3) "tag" [5]=> string(10) "give chase" [6]=> string(8) "go after" [7]=> string(5) "track" [8]=> string(6) "follow" [9]=> string(6) "pursue" } ["rel"]=> array(2) { [0]=> string(10) "chase away" [1]=> string(9) "tag along" } } }

if ($_POST['query'])
{
    $word = ($_POST['query']);
    $fullquery = 'http://words.bighugelabs.com/api/2/2b1ae894d9e4acd1517b457666fc9431/'.$word.'/json';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $fullquery);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data=curl_exec($ch);
    $js = json_decode($data);
    
    var_dump($js);
    $ob_c=0;
    foreach ($js -> noun as $object[$ob_c])
    {
    echo $object[$ob_c].'<br>';
    $ob_c++;
    }
}

I don't follow .... I have parsed similar files before like this which works

        curl_setopt($ch, CURLOPT_URL, $fullUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $data=curl_exec($ch);
        $js = json_decode($data);
        $blekko_count=0;
        $rank=100;
        foreach ($js->RESULT as $item)
        {
        
                         $Blekko[$blekko_count] =  array 
                        ('url'=>$item->url, 
                         'url_title' =>$item->url_title,     
                         'snippet' =>$item->snippet,
                         'rank' => 100 - $blekko_count,
                         'engine' => 'Blekko'); 
                         $blekko_count++;

you can get the data directly as an array by setting the parameter in json_decode(), no need to parse anything unless you have some specific requirements, which you didn't state.

 

all you stated is you want to get the posted wall of object data (if you echo a '<pre>' tag before that, it will be formatted as a readable wall of object data) into an array without any indication of what you wanted to parse or of what problem you had when you tried to parse it.

Ok, I understand. Thank you for your reply. 

 

I was actually looking to parse the synonyms offered by the var dump in to an array. "Domestic dog[1]" and "Caninis Familirialis[2]". So I was attempting to have each one of these synonyms as $object[$ob_c] and echo them. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.