1

So I need a function to be able to add elements to a associative array, along with a counter to count the number of this function called. Here is what I got so far:

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

Obviously it is wrong, can someone let me know where and how to do this properly? Thanks

2
  • Why do you need to write a function for this? Why not just do Globals[$courseCode] = $courseName instead of calling your functions. Then just do echo 'You have a total of ' . count($Globals) . ' courses now!'; Commented Sep 22, 2017 at 19:49
  • I don't know what you're trying to accomplish with return $course(), I imagine, you should drop off the () since I don't think that's a function. Commented Sep 22, 2017 at 19:50

3 Answers 3

1

There are many problems in your code.

  • You return a function with an array ? I don't see what you're trying to do here, simply return the array.

return $course();

to

return $course;

  • You return before you actually increment the counter variable, so it's never getting incremented !

So :

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

to

$course[courseCode] = $courseName;
$counter++;
return $course;
  • You're not passing that counter variable anywhere. To do what you want, you need to pass it to the function by reference.

So :

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

to

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

Here's the final code fixed :

<?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!';
Sign up to request clarification or add additional context in comments.

Comments

1

Here is an answer with explanations in the code

<?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 Comments

It's good practice to use {} around variables when echo'ing them out. In this case it works fine, but still fwiw.
Thanks for the answer, your code is working as well.
-1

This is very strange way of doing this. If you absolutely have to do it in the way your code alludes, this will work:

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

That said, a much cleaner (imo) way of doing this would be:

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

4 Comments

I was writing the same cleaner solution out as you posted this. I agree, I don't see the purpose of the function or adding the count to the array when you can use the count() function.
I upvoted it, someone else downvoted. If you were directing that at me...
Not directed at you. Was just wondering if whoever did was still around
I agree, but the requirement require me to use function to acomplish that. And this is the only way I can think of...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.