About PHP arrays?

  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

Lets say i want to execute blocks of code based on the values of an array. Why then doesn't the below example code work?

Code: [ Select ]
<?php
$numbers = array("1","5","7");

switch ($numbers)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
case 4:
echo "Number 4";
break;
case 5:
echo "Number 5";
break;
case 6:
echo "Number 6";
break;
case 7:
echo "Number 7";
break;
case 8:
echo "Number 8";
break;
default:
echo "No number between 1 and 3";
}
?>
  1. <?php
  2. $numbers = array("1","5","7");
  3. switch ($numbers)
  4. {
  5. case 1:
  6. echo "Number 1";
  7. break;
  8. case 2:
  9. echo "Number 2";
  10. break;
  11. case 3:
  12. echo "Number 3";
  13. break;
  14. case 4:
  15. echo "Number 4";
  16. break;
  17. case 5:
  18. echo "Number 5";
  19. break;
  20. case 6:
  21. echo "Number 6";
  22. break;
  23. case 7:
  24. echo "Number 7";
  25. break;
  26. case 8:
  27. echo "Number 8";
  28. break;
  29. default:
  30. echo "No number between 1 and 3";
  31. }
  32. ?>


With this code i should get: "Number 1 Number 5 Number 7" instead i get the default "No number between 1 and 3". Its as if the array isn't even there. Any ideas on what I am doing wrong?
  • Flanders
  • Beginner
  • Beginner
  • User avatar
  • Posts: 48
  • Loc: Reno, Nevada US

Post 3+ Months Ago

Well, the first thing is the break statement would return only the first match and then break. Second, I would think that the case statement should be checking for strings (i.e. "1" and "5", not 1 and 5) since that is how the array is defined. As for using switch statements with an array, I don't know if that is possible. From the PHP.net:

Quote:
The case expression may be any expression that evaluates to a simple type, that is, integer or floating-point numbers and strings. Arrays or objects cannot be used here unless they are dereferenced to a simple type.


I would think looping through the array and running the switch statement would be the way to go.

Code: [ Select ]
<?php
$numbers = array("1","5","7");
function switch_array($arr){
for($i=0;$i<sizeof($arr);$i++){
switch ($arr[$i])
{
case "1":
echo "Number 1";
break;
 
case "2":
echo "Number 2";
break;
 
case "3":
echo "Number 3";
break;
 
case "4":
echo "Number 4";
break;
 
case "5":
echo "Number 5";
break;
 
case "6":
echo "Number 6";
break;
 
case "7":
echo "Number 7";
break;
 
default:
echo "No Numbers between 1 and 7";
}
}
}
switch_array($numbers);  //Outputs Number 1Number 5Number 7
?>
  1. <?php
  2. $numbers = array("1","5","7");
  3. function switch_array($arr){
  4. for($i=0;$i<sizeof($arr);$i++){
  5. switch ($arr[$i])
  6. {
  7. case "1":
  8. echo "Number 1";
  9. break;
  10.  
  11. case "2":
  12. echo "Number 2";
  13. break;
  14.  
  15. case "3":
  16. echo "Number 3";
  17. break;
  18.  
  19. case "4":
  20. echo "Number 4";
  21. break;
  22.  
  23. case "5":
  24. echo "Number 5";
  25. break;
  26.  
  27. case "6":
  28. echo "Number 6";
  29. break;
  30.  
  31. case "7":
  32. echo "Number 7";
  33. break;
  34.  
  35. default:
  36. echo "No Numbers between 1 and 7";
  37. }
  38. }
  39. }
  40. switch_array($numbers);  //Outputs Number 1Number 5Number 7
  41. ?>
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

Wow, thanks so much, it works great. Yeah, I didn't know if the switch function could handle arrays or not.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 113 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.