2017-09-22 127 views
0

所以我需要一個函數能夠將元素添加到關聯數組,以及計數器來計算此函數調用的數量。 這是我走到這一步:如何在php中使用函數添加關聯數組?

<?php 
    //1 
    $Globals = []; 
    $counter = 0; 
    function array_push_assoc($course, $courseCode, $courseName){ 
    $course[courseCode] = $courseName; 
    return $course(); 
    $counter ++; 
} 
    $Globals = array_push_assoc($Globals, 'CIS370', 'Introduction to Web Development'); 
    $Globals = array_push_assoc($Globals, 'CIS475', 'Advance Web Development'); 
    $Globals = array_push_assoc($Globals, 'CIS560', 'Introduction to Syber Security'); 
    $Globals = array_push_assoc($Globals, 'CIS564', 'Hacking Technic'); 
    //2 
    echo 'You have a total of $counter courses now!'; 
?> 

顯然,這是錯誤的,有人可以讓我知道在哪裏以及如何做到這一點正常嗎? 謝謝

+0

爲什麼你需要爲此編寫一個函數?爲什麼不只是'Globals [$ courseCode] = $ courseName'而不是調用你的函數。然後只是'echo'你總共有'。計數($ Globals)。 ''courses now!';' – GentlemanMax

+0

我不知道你想用'return $ course()'完成什麼,我想你應該放棄'()',因爲我不認爲這是一個函數。 – bc2946088

回答

0

你的代碼有很多問題。

  • 您返回一個函數與數組?我沒有看到你要在這裏做什麼,只需返回數組。

return $course();

return $course;

  • 你回來之前,你實際上會增加計數器變量,所以它從來沒有得到遞增!

所以:

$course[courseCode] = $courseName; 
return $course; 
$counter ++; 

$course[courseCode] = $courseName; 
$counter++; 
return $course; 
  • 你不能傳遞一個計數器變量的任何地方。要做你想做的事,你需要通過引用將它傳遞給函數。

所以:

function array_push_assoc($course, $courseCode, $courseName) 
{ 
    $course[$courseCode] = $courseName; 
    $counter++; 
    return $course; 
} 

function array_push_assoc($course, $courseCode, $courseName, &$counter) 
{ 
    $course[$courseCode] = $courseName; 
    $counter++; 
    return $course; 
} 

這裏是固定的最終代碼:

<?php 
//1 
$Globals = []; 
$counter = 0; 
function array_push_assoc($course, $courseCode, $courseName, &$counter) 
{ 
    $course[$courseCode] = $courseName; 
    $counter++; 
    return $course; 
} 

$Globals = array_push_assoc($Globals, 'CIS370', 'Introduction to Web Development', $counter); 
$Globals = array_push_assoc($Globals, 'CIS475', 'Advance Web Development', $counter); 
$Globals = array_push_assoc($Globals, 'CIS560', 'Introduction to Syber Security', $counter); 
$Globals = array_push_assoc($Globals, 'CIS564', 'Hacking Technic', $counter); 
//2 
echo 'You have a total of $counter courses now!'; 
0

這是做這個的非常奇怪的方式。如果你絕對必須這樣做,在你的代碼暗指的方式,這將工作:

$Globals = []; 
$counter = 0; 

function array_push_assoc($course, $courseCode, $courseName, &$count){ 
    $course[$courseCode] = $courseName; 
    $count++; 
    return $course; 
} 

$Globals = array_push_assoc($Globals, 'CIS370', 'Introduction to Web Development', $counter); 
$Globals = array_push_assoc($Globals, 'CIS475', 'Advance Web Development', $counter); 
$Globals = array_push_assoc($Globals, 'CIS560', 'Introduction to Syber Security', $counter); 
$Globals = array_push_assoc($Globals, 'CIS564', 'Hacking Technic', $counter); 

echo "You have a total of {$counter} courses now!"; 

那說,這樣做的更清潔(IMO)的方法是:

$Globals = []; 
$Globals['CIS370'] = 'Introduction to Web Development'; 
$Globals['CIS475'] = 'Advance Web Development'; 
$Globals['CIS560'] = 'Introduction to Syber Security'; 
$Globals['CIS564'] = 'Hacking Technic'; 

echo 'You have a total of '. count($Globals) .' courses now!'; 
+0

當您發佈此消息時,我正在寫相同的*清理程序*解決方案。我同意,當您可以使用count()函數時,我沒有看到該函數的用途或將數添加到數組中。 – bc2946088

+0

爲什麼downvote? – GentlemanMax

+0

我投了票,其他人低估了。如果你是在指導我...... – bc2946088

0

這裏在代碼中有解釋的答案

<?php 

    $Globals = []; 
    $counter = 0; 

    /** 
    * @param array $course 
    * @param string $courseCode 
    * @param string $courseName 
    * @param int $counter 
    * 
    * @return array 
    */ 
    function array_push_assoc($course, $courseCode, $courseName, $counter){ 
     $course[$courseCode] = $courseName; 

     // inside a function, you cannot use a global variable, you have to get it as argument and return it 
     $counter++; 

     // do the return at the end of the function because nothing else is performed after this 
     return array(
      $course, 
      $counter 
     ); 
    } 

    list($Globals, $counter) = array_push_assoc($Globals, 'CIS370', 'Introduction to Web Development', $counter); 
    list($Globals, $counter) = array_push_assoc($Globals, 'CIS475', 'Advance Web Development', $counter); 
    list($Globals, $counter) = array_push_assoc($Globals, 'CIS560', 'Introduction to Syber Security', $counter); 
    list($Globals, $counter) = array_push_assoc($Globals, 'CIS564', 'Hacking Technic', $counter); 

    // use double quotes "" if you want $counter to be echoed as the value of the variable $counter, not the word '$counter' 
    echo "You have a total of $counter courses now!"; 
    // it is good practice to add a line break, this improves the script output 
    echo PHP_EOL; 
?> 
+0

當回顯變量時,在變量周圍使用'{}'是個好習慣。在這種情況下,它可以很好地工作,但仍然有效。 – bc2946088

+0

感謝您的回答,您的代碼也在工作。 –