0
function get_frequencies( $a )
{
  $get_frequencies = array();
  foreach( $a as $k => $v )
  { 
    $get_frequencies[$v]++ ;  //this is the line causing the error
  }
  return $get_frequencies;
}
/*Get Flip function involking and testing */        
$letter_freq = array("a" => "x", "c" => "y", "b" => "z", "d" => "y", "z" => "y");
$get_frequencies = get_frequencies( $letter_freq );
print_r($get_frequencies)

Heres the error im getting the answer is correct but still getting this error.

Notice: Undefined index: x in
C:\Users\Marty2\Desktop\xampp\htdocs\lab13\array_library.php on line
235

Notice: Undefined index: y in
C:\Users\Marty2\Desktop\xampp\htdocs\lab13\array_library.php on line
235

Notice: Undefined index: z in
C:\Users\Marty2\Desktop\xampp\htdocs\lab13\array_library.php on line
235
Array ( [x] => 1 [y] => 3 [z] => 1 )
Zombo
  • 1
  • 62
  • 391
  • 407

1 Answers1

2

Because you're trying to increment values in variables that don't exist yet. Just check to make sure they exist, and if not, instantiate them and assign them a value of zero. Then you can safely increment their value.

if (!isset($get_frequencies[$v]))
{
    $get_frequencies[$v] = 0;
}
$get_frequencies[$v]++;
John Conde
  • 217,595
  • 99
  • 455
  • 496