Adam P
Adam P

Reputation: 139

Outputing multidimensional array - many levels

I am looking for a way to output a multidimensional array as a simple table.

I got it by processing xml available here http://google.com/complete/search?output=toolbar&q=adam with the simple function

function convertXmlObjToArr($obj) { 
$json = json_encode($obj);
$array = json_decode($json,TRUE);

return $array;

}

and got an array

   array(1){ ["CompleteSuggestion"]=>
  array(10) {
    [0]=>
    array(1) {
      ["suggestion"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["data"]=>
          string(12) "adam sandler"
        }
      }
    }
    [1]=>
    array(2) {
      ["suggestion"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["data"]=>
          string(11) "adam levine"
        }
      }
      ["num_queries"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["int"]=>
          string(8) "33200000"
        }
      }
    }
    [2]=>
    array(2) {
      ["suggestion"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["data"]=>
          string(12) "adam carolla"
        }
      }
      ["num_queries"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["int"]=>
          string(7) "5570000"
        }
      }
    }
    [3]=>
    array(2) {
      ["suggestion"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["data"]=>
          string(12) "adam lambert"
        }
      }
      ["num_queries"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["int"]=>
          string(8) "45400000"
        }
      }
    }
    [4]=>
    array(1) {
      ["suggestion"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["data"]=>
          string(17) "adam sandler died"
        }
      }
    }
    [5]=>
    array(2) {
      ["suggestion"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["data"]=>
          string(19) "adam sandler movies"
        }
      }
      ["num_queries"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["int"]=>
          string(8) "43600000"
        }
      }
    }
    [6]=>
    array(2) {
      ["suggestion"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["data"]=>
          string(13) "adam morrison"
        }
      }
      ["num_queries"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["int"]=>
          string(8) "13800000"
        }
      }
    }
    [7]=>
    array(2) {
      ["suggestion"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["data"]=>
          string(10) "adams golf"
        }
      }
      ["num_queries"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["int"]=>
          string(8) "30500000"
        }
      }
    }
    [8]=>
    array(2) {
      ["suggestion"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["data"]=>
          string(10) "adam yauch"
        }
      }
      ["num_queries"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["int"]=>
          string(8) "19700000"
        }
      }
    }
    [9]=>
    array(2) {
      ["suggestion"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["data"]=>
          string(9) "adam west"
        }
      }
      ["num_queries"]=>
      array(1) {
        ["@attributes"]=>
        array(1) {
          ["int"]=>
          string(9) "137000000"
        }
      }
    }
  }
}

Now I would like to output it and face 2 problems:

  1. How do I "dig into" the relevant information ("data" and "int") in an array structured like this? So far I was dealing with much simpler arrays (not so "deep"). I am trying to for it via foreach loop

foreach($googleKeywords->CompleteSuggestion as $suggestion){

echo"{$this->suggestion->data}"; }

but keep getting "Trying to get property of non-object" error.

  1. As you notice some of the data is not complete - there is a name in "data" but not corresponding "int"... would be there a way to echo "no data" whether "int" is not set?

All the best,

Adam

Upvotes: 0

Views: 169

Answers (2)

Adam P
Adam P

Reputation: 139

Thanks,

The final code that worked involved nested loops and is as follows:

<pre>foreach($keywords as $suggestion){
 foreach ($suggestion as $finalSuggestion){
   echo "<p>".$finalSuggestion['suggestion']['@attributes']['data']."</p>";
   echo $finalSuggestion['num_queries']['@attributes']['data']."<br/>";
    if (array_key_exists('num_queries', $finalSuggestion)) {
    echo "<p> ".$finalSuggestion['num_queries']['@attributes']['int']." </p>";
    }
    else
        {
        echo "<p>no data</p> ";
    }
     }

}
</pre>  

PS. Sorry for the awful formatting of the code , still not sure how to do it properly here.... I usually just put < pre> but it goes wild now....

Upvotes: 0

Luke Mills
Luke Mills

Reputation: 1606

You could try this (Code updated):

foreach($googleKeywords['CompleteSuggestion'] as $suggestion){
    if (count($suggestion) == 2) {
        echo $suggestion['suggestion']['@attributes']['data'];
        echo $suggestion['num_queries']['@attributes']['int'];
    }
}

This should at least give you in idea on how to access the elements of your array.

I'll leave it up to you to format the output.

The if statement checks that all data is set (you expect 2 items, both data and int). You could have an else{} section that echoes "data not set". The for loop is necessary because you don't know how many elements, and this style of for loop works that out for you. It will loop over all the elements in the array, whether there is only 1, or 9, or whatever. If there are zero elements in the array, the for loop will be skipped.

Hope this helps.

Upvotes: 1

Related Questions