Jump to content

php reports undefined array index, but print_r shows the indexes ???


fotobleu

Recommended Posts

???

 

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

 

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'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.