I am using a foreach construct to look at an array returned from a function. Unfortunately, this function returns different structures if there is only 1 result than if there are more than 1 result. When there is a single result, I get the following:
array(3) {
["name"]=>
string(5) "chris"
["admin"]=>
string(5) "chris"
["time"]=>
string(19) "2014/06/27 12:36:31"
}
string(34) "$1$9243ujf0i2j8ehdf24hdf9a8"
I am trying to get the ["name"] value out. If I use the following code, my variable has the right data but I get an Illegal String Offset error:
foreach($res[1]['result']['user']['entry'] as $user) {
$s = $user['name'];
echo $s;
}
How do I properly get to the array["name"] value? Or do I have to do something different at a higher level? The raw data I get back is as follows when there is a single entry:
array(2) {
["@attributes"]=>
array(2) {
["status"]=>
string(7) "success"
["code"]=>
string(2) "19"
}
["result"]=>
array(2) {
["@attributes"]=>
array(2) {
["total-count"]=>
string(1) "1"
["count"]=>
string(1) "1"
}
["user"]=>
array(2) {
["@attributes"]=>
array(2) {
["admin"]=>
string(5) "chris"
["time"]=>
string(19) "2014/06/27 12:39:58"
}
["entry"]=>
array(2) {
["@attributes"]=>
array(3) {
["name"]=>
string(5) "chris"
["admin"]=>
string(5) "chris"
["time"]=>
string(19) "2014/06/27 12:36:31"
}
["phash"]=>
string(34) "$1$9243ujf0i2j8ehdf24hdf9a8"
}
}
}
}
and the following when there is more than one entry:
array(2) {
["@attributes"]=>
array(2) {
["status"]=>
string(7) "success"
["code"]=>
string(2) "19"
}
["result"]=>
array(2) {
["@attributes"]=>
array(2) {
["total-count"]=>
string(1) "1"
["count"]=>
string(1) "1"
}
["user"]=>
array(2) {
["@attributes"]=>
array(2) {
["admin"]=>
string(5) "chris"
["time"]=>
string(19) "2014/06/27 12:57:32"
}
["entry"]=>
array(2) {
[0]=>
array(2) {
["@attributes"]=>
array(3) {
["name"]=>
string(5) "chris"
["admin"]=>
string(5) "chris"
["time"]=>
string(19) "2014/06/27 12:36:31"
}
["phash"]=>
string(34) "$1$9243ujf0i2j8ehdf24hdf9a8"
}
[1]=>
array(2) {
["@attributes"]=>
array(3) {
["name"]=>
string(4) "test"
["admin"]=>
string(5) "chris"
["time"]=>
string(19) "2014/06/27 12:57:32"
}
["phash"]=>
string(34) "$1$as9d8jf238r9jf89j9238jr"
}
}
}
}
}
Note the extra indexed array level after ["entry"]. Basically, I just want the list of ["name"] values.
foreach($res[1]['result']['user']['entry']['@attributes'] as $user). Both of them hold thenamebit inside that sub-array.@attributesto either path. Where does the array come from?