如何在php中使用函数添加关联数组?

时间:2017-09-22 19:43:53

标签: php arrays

所以我需要一个能够将元素添加到关联数组的函数,以及一个计数器来计算所调用的函数的数量。 这是我到目前为止所得到的:

<?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!';
?>

显然这是错误的,有人能让我知道在哪里以及如何正确地做到这一点? 感谢

3 个答案:

答案 0 :(得分: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!';

答案 1 :(得分: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;
?>

答案 2 :(得分:-1)

这是非常奇怪的做法。如果你必须以你的代码所暗示的方式来做,这将有效:

$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!';