fotobleu Posted March 24, 2008 Share Posted March 24, 2008 ??? hi, i made this function to decode a one level json string function decode($jsonToArr) { $newStr = substr($jsonToArr,1,(strlen($jsonToArr) - 1)); $exArr = explode(",",$newStr); $i = 0; $exArrCount = count($exArr); while($i < $exArrCount) { $splitArr = explode(":",$exArr[$i]); $val = ($splitArr[1]) ? $splitArr[1] : ''; $retArr[$splitArr[0]] = $val; $i++; } return $retArr; } the json tring is this : $testStr = '{"camera":nikon,"aperture":f3.5,"exposure":1/250}'; then i feed it trough my simple decode function $exifArr = decode($testStr); then i try to print the camera echo $exifArr['camera']; but no output is found but when i do print_r($exifArr); i get all the output out, the indexes and the values, along with a 1 at the end does anyone know how come i cannot access the array values this way : echo $exifArr['camera']; thanks Link to comment https://forums.phpfreaks.com/topic/97588-php-reports-undefined-array-index-but-print_r-shows-the-indexes/ Share on other sites More sharing options...
Barand Posted March 24, 2008 Share Posted March 24, 2008 The key contains the "s echo $exifArr['"camera"']; Link to comment https://forums.phpfreaks.com/topic/97588-php-reports-undefined-array-index-but-print_r-shows-the-indexes/#findComment-499308 Share on other sites More sharing options...
fotobleu Posted March 24, 2008 Author Share Posted March 24, 2008 thanks for the reply what if the key would be 'key' instead of "key" how would i reference it? thanks Link to comment https://forums.phpfreaks.com/topic/97588-php-reports-undefined-array-index-but-print_r-shows-the-indexes/#findComment-499313 Share on other sites More sharing options...
wildteen88 Posted March 24, 2008 Share Posted March 24, 2008 I have cleaned your decode function: function decode($jsonToArr) { $jsonToArr = str_replace(array('"', '\'', '{', '}'), '', $jsonToArr); $bits = explode(',', $jsonToArr); $Arr = array(); foreach($bits as $part) { list($key, $value) = explode(':', $part); $Arr[$key] = $value; } return $Arr; } $testStr = '{"camera":nikon,"aperture":f3.5,"exposure":1/250}'; $exifArr = decode($testStr); echo $exifArr['camera']; Link to comment https://forums.phpfreaks.com/topic/97588-php-reports-undefined-array-index-but-print_r-shows-the-indexes/#findComment-499360 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.