PHP 5.4 Illegal String Offset Error -
i using foreach construct @ array returned function. unfortunately, function returns different structures if there 1 result if there more 1 result. when there single result, 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 trying ["name"] value out. if use following code, variable has right data illegal string offset error:
foreach($res[1]['result']['user']['entry'] $user) { $s = $user['name']; echo $s; }
how array["name"] value? or have different @ higher level? raw data follows when there 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 following when there more 1 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 indexed array level after ["entry"]. basically, want list of ["name"] values.
the service returns different structure depending on number of results broken begin with. if cannot fixed, try normalize data before processing this:
if (!isset($res[1]['result']['user']['entry'][1]) { // not array, change structure array 1 element: $res[1]['result']['user']['entry'] = array($res[1]['result']['user']['entry']); } // process data if service returning array
Comments
Post a Comment