2017-09-22 124 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

感谢您的回答,您的代码也在工作。 –