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:
- 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.
- 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
Thanks,
The final code that worked involved nested loops and is as follows:
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….