0

I'm trying to simply add +1 to an array like this

$game['teamA']['goals']++;

But then it'll throw me an error about undefined index. And I guess that's because $game['teamA']['goals'] is not set before I am adding a value to it.

But isn't there a way to workaround this?

I could set the $game['teamA']['goals'] = 0 in start of simulation, but then I have to do that with freekicks, penalties, cards etc., AND do it for both teams. Isn't there another way of doing this?

Thanks in advance :)

4
  • so.. checking if it isset, if not add? Or is there a shorten way of using isset? Commented Oct 13, 2012 at 12:58
  • Are you asking if there's a way to test if it's already a number, before incrementing it? Or are you asking if there's some way of convincing the compiler that all unknown values should be treated as 0? Commented Oct 13, 2012 at 12:59
  • @Sepster that all unknown values should be treated as 0 before incrementing. Commented Oct 13, 2012 at 12:59
  • 2
    @Kolind that would be asking for trouble. Zero (in most cases - including yours, I'd say!) has meaning too. So by confusing zero with "unknown" or "undefined", would ruin the meaning of zero. Commented Oct 13, 2012 at 13:04

4 Answers 4

2

You should ALWAYS initialise variables before they are used. One way to do this would be:

$game = array('teamA' => 
                  array(
                      'goals' => 0,
                      'freekicks' => 0,
                      'penalties' => 0,
                      ....
                  ),
              'teamB' =>
                  array(
                      'goals' => 0,
                      'freekicks' => 0,
                      'penalties' => 0,
                      ...
                  )
              );
Sign up to request clarification or add additional context in comments.

1 Comment

I will initialise them to begin win then. Seems this is proper way of doing it. Thanks :)
2

I'd suggest you go with a "proper initialisation", as you suggested. That would some code to your program but it's always better then on each update to check. Better do

$game = array( 'teamA' => array('goals'       => 0,
                                'freekicks'   => 0,
                                'penalties'   => 0,
                                // etc ...                       
                          ),
               'teamB' => array('goals'       => 0,
                                'freekicks'   => 0,
                                'penalties'   => 0,
                                // etc ...
                          )
        );  

instead of

if(isset($game['teamA']['goals']))
    $game['teamA']['goals']++;
else
    $game[teamA]['goals'] = 1;

There's nothing wrong to properly init your variables - all of them. It's even advisable to do so.

1 Comment

Thanks. But i'll initialise them in the beginning. I know. I agree on you :)
1

write a wrapper function to set the values.

<?php

function increment(&$dict, $key1, $key2) {
  if (!isset($dict, $key1))
    $dict[$key1] = array();
  if (!isset($dict[$key1], $key2))
    $dict[$key1][$key2] = 1;
  else
    $dict[$key1][$key2] ++;
}

$game= array();
increment($game, "teamA", "goals");
var_dump( $game);
?>

It really depends on what you expect on the runtime; if these operations are called often, it would be better to create all array indices before doing anything else, like https://stackoverflow.com/a/12873359/1689451 suggested.

Comments

0

You can do one thing. Before incrementing, check for existence of the array, if not, create one! As simple as that. You can use a built-in function called isset(), which checks the existence of the variable.

Short Way:

(isset($game['teamA']['goals']) ? $game['teamA']['goals']++ : $game['teamA']['goals'] = 1;

Or using if condition:

if (isset($game['teamA']['goals'])
    $game['teamA']['goals']++;
else
    $game['teamA']['goals'] = 1;

4 Comments

I know isset() exists. But did not know the shorten version of an if-sentence. I'll use that. I was looking for something as short as possible.
Once again - better initialise them in the beginning.
This is the best shorter way possible. Try it out, else I will try to get more shorter way.
Thanks @Praveen but I will initialise my $game array in the beginning of my script.

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.