2013-02-03 2 views
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) 

답변을 얻는 중 오류가 발생했지만 여전히이 오류가 발생합니다.내 함수에서 정의되지 않은 색인 오류가 발생했습니다.

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) 

답변

2

아직 존재하지 않는 변수의 값을 늘리려고하기 때문에. 그것들이 존재하는지 확인하고 그렇지 않다면, 그것들을 인스턴스화하고 그들에게 0의 값을 할당하십시오. 그런 다음 안전하게 가치를 올릴 수 있습니다.

if (!isset($get_frequencies[$v])) 
{ 
    $get_frequencies[$v] = 0; 
} 
$get_frequencies[$v]++; 
+0

빠른 답장을 보내 주셔서 감사합니다. –

관련 문제