1

I am getting error

"Undefined index at Line Number: 233" which is

if($rule[$item] == '@' && isset($keys[$index + 1])) 

and another at line 244 which is

$keys);

Function which is called is:

public function preeti()
{
$rule =
    [
        "c" => "d",
        "a" => "o",
        "t" => "g",
        "h" => "a",
        "1" => "@",
        "e" => "n",
        "n" => "t"
    ];

    $input = $this->input->get('preeti');
    $keys = str_split($input);

    $output = [];
    array_walk($keys,
    function($item, $index) use($rule,$keys, &$output) {
        if($rule[$item] == '@' && isset($keys[$index + 1])) {
            $output[] = $rule[$keys[$index + 1]];
            return;
        }
        if(isset($keys[$index - 1]) && $rule[$keys[$index - 1]] == '@') {
            $output[] = '@';
            return;
        }
        $output[] = $rule[$item] ?? null;
        return;
    },
    $keys);

    $final_output = implode($output);

    $this ->load->blade('index.preeti-to-unicode',[
        'preeti' => $input,
        'unicode' => $final_output,
    ]);
}

When I try to load view page (using CodeIgniter framework) calling preeti() function,it shows the following error. Screenshot :

enter image description here

I think the error is because of missing ?? null somewhere because maybe it can't handle input value which can't be found in an array.

Danish Ali
  • 2,354
  • 3
  • 15
  • 26
ram shah
  • 81
  • 7

1 Answers1

1

If the current $item of your $input is not a key of $rule you will get this undefinded index exception. You need to also test isset($rule[$item]).

You can remove the third parameter of array_walk ($keys). This would be the third parameter of your callback function which you don't have defined.

array_walk($keys,
function($item, $index) use($rule,$keys, &$output) {
    if(isset($rule[$item]) && $rule[$item] == '@' && isset($keys[$index + 1])) {
        $output[] = $rule[$keys[$index + 1]];
        return;
    }
    if(isset($keys[$index - 1]) && $rule[$keys[$index - 1]] == '@') {
        $output[] = '@';
        return;
    }
    $output[] = $rule[$item] ?? null;
    return;
});
user3190433
  • 375
  • 3
  • 7
  • Your code works perfect but if I give input value which is not defined in hashmap array $rule,it shows undefined index. i.e if I give input as 's',it shows Message: Undefined index: s – ram shah Dec 14 '18 at 08:37