-1

I have an array.

$arrayVar = array(8, 10, 'u'=>24, 'm'=>45, 54, 45);

foreach($arrayVar as $curK=>$curV)

  switch($curK)
  {
      case 'u':

            echo $curK;  //statement

            break;

      default:

             //statements
  }

Why are the statements under the first case getting executed for all $curK where the array key has not been specified apart from when the label matches the value of $curK?

I did try overcoming the problem by checking $curK for empty values

if(!empty($curK)) {

  foreach($arrayVar as $curK=>$curV)

    switch($curK)
    {
        case 'u':

            echo $curK;  //statement

            break;
    }
}

...

   $arrayVar = array('a'=>8400,'u'=>1100,1300,1400,true);

   echo '<pre>'; print_r($arrayVar);

    foreach($arrayVar as $curK=>$curDU)
    {
        echo "\n" . $curK . "\t";

        switch($curK)
        {
            case 'a':

                echo 'When a : ' . $curK . "\n";

                break;

            case 'u':

                echo 'When u : ' . $curK . "\n";

                break;

            default:
        }
    }

This is the result i got

Array ( [a] => 8400 [u] => 1100 [0] => 1300 [1] => 1400 [2] => 1 )

a When a : a

u When u : u

0 When a : 0

1
2

The problem arose with the php assigned key 0.

2 Answers2

2

Every array element has a key. If you loop through your current array and echo out each key you will get the following:

$arrayVar = array(8, 10, 'u'=>24, 'm'=>45, 54, 45);

foreach($arrayVar as $curK=>$curV)
{
  echo $curK, ' ';
}

//outputs: 0 1 u m 2 3 

Demo

As you can see the array elements that have no specified key are given the next available numerical index value (which starts as zero for the first one).

Your second example won't work because:

  1. You're checking the value of a variable that doesn't yet exist (and PHP will give you a notice) and you will never enter that code as a result.

  2. 0 will evaluate to true when passed to empty() which, because as I have explained above array values that have no key will get numerically keyed, the first value will have a key of zero which will become false in your if statement which is probably not what you want.

What you are probably looking for is making sure the array item has a non-numeric key before acting on it:

foreach($arrayVar as $curK=>$curV) {
  if(!is_int($curK)) {
    switch($curK)
    {
        case 'u':
        case 'm':
            echo $curK;  //statement
            break;
        default:
            // do somethng
    }
  }
}

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • I get the error i committed with the solution I was trying to adopt My doubt is - Why are the statements under the first case label ('u' in this case) getting executed for all $curK where the array key has not been specified? – The Edifier Jul 24 '16 at 00:50
  • That shouldn't be happening with the code you posted. – John Conde Jul 24 '16 at 00:52
  • I couldn't find a way to add code in the comment. I have edited the question to add the code that I used to retest. – The Edifier Jul 24 '16 at 01:14
  • [Looks like there's a bug in PHP](https://eval.in/610837) as 0 clearly is not equal to 'a' – John Conde Jul 24 '16 at 01:23
  • Thanks. I was trying to learn and find out if I was not aware of something. – The Edifier Jul 24 '16 at 01:30
  • @TheEdifier and John. This is not a bug. `switch` uses loose comparison so when the key is 0, it tests `0=='a'` which is true and thus echoes the key. (see the 2nd highlighted note in the docs: http://php.net/manual/en/control-structures.switch.php). A strict `===` comparison would yield a different result. See this fork of John's demo: https://eval.in/610852 – BeetleJuice Jul 24 '16 at 02:41
  • Is there a way to make use of switch in the context relevant to the case, may be like using a case 0 without any statements relating to it? Wouldn't case '0' be inappropriate then. – The Edifier Jul 24 '16 at 03:51
2

Every array element has a key, even if you didn't declare it explicitly. To see them, use the array_keys function. Consider this array:

$arr = ['apple','banana', 'favorite' => 'plum'];

It may look like favorite is the only key, but that's not true:

$keys = array_keys($arr);
print_r($keys);

Result:

[
    0 => 0,
    1 => 1,
    2 => 'favorite',
]

As you can see, the keys are 0, 1, and favorite. When a key is not specified, PHP creates one and keeps incrementing, starting with the highest current integer key. If there is no integer key, it starts with 0.

Understanding arrays is fundamental in PHP. Please read the manual.

BeetleJuice
  • 39,516
  • 19
  • 105
  • 165