Here is what I would do and it should work regardless of values in $str:
function replace_str($str,$search,$replace,$num) {
    $pieces = explode(',',$str);
    $counter = 0;
    foreach($pieces as $key=>$val) {
        if($val == $search) {
            $counter++;
            if($counter == $num) {
                $pieces[$key] = $replace;
            }
        }
    }
    return implode(',',$pieces);
}
$str="0000,1023,1024,1025,1024,1023,1027,1025,1024,1025,0000";
echo replace_str($str, '1024', 'JJJJ', 3);
I think this is what you are asking in your comment:
function replace_element($str,$search,$replace,$num) {
    $num = $num - 1;
    $pieces = explode(',',$str);
    if($pieces[$num] == $search) {
        $pieces[$num] = $replace;
    }
    return implode(',',$pieces);
}
$str="0000,1023,1024,1025,1024,1023,1027,1025,1024,1025,0000";
echo replace_element($str,'1024','JJJJ',9);