Jump to content

How would I use a switch case statement to run through an array?


Stuart_Westgate

Recommended Posts

I'm trying to use an array to go through a switch case statement.

This is my code at present but I feel something is missing as it's not working > Can someone please help?

 

 

$stringArray = array('socks:', '', 'jacket', 'boxers', 'trainer', 'tshirt', 'bag', 'tie');

 

$pos = strpos($statusItemCheck, $stringArray[0]);

 

$imageValue = "images/"; //Value of

 

switch($pos){

case $stringArray[0]:

$imageValue .= "socks.PNG";

break;

case $stringArray[1]:

$imageValue .= "hat.PNG";

break;

case$stringArray[2]:

$imageValue .= "jacket.PNG";

break;

default:

$imageValue .= "questionMark.PNG";

break;

}

Link to comment
Share on other sites

Try something like this:

<?PHP

//### Set this for testing purpose
$statusItemCheck = 'jacket';

//### Path of images
$imageValue = 'images/';

//### Array of items
$stringArray = array('socks' => 'socks.png',
				 'hat' => 'hat.png',
				 'jacket' => 'jacket.png',
				 'boxers' => 'boxers.png'
				 );

//### Check if the item exists in the array
if(!isSet($stringArray[$statusItemCheck])) {
echo 'Item does not exist.';

//### Item exists, add the image filename to image value
} else {	
$imageValue .= $stringArray[$statusItemCheck];

echo $imageValue;
}

?>

Edited by PaulRyan
Link to comment
Share on other sites

If the pics are always going to have the standard name format of "item".png, you could do this

 

$availableItemsArray = ("socks", "jacket", "hat", "boxers");

 

$testItemsArray = ("jacket", "boxers", "monkey hair gloves", "socks", "jodhpurs");

 

$arrayLength = count($itemsArray); // you could put this in the for loop below .. but its better to only count the array one time and save it, than to count it on each iteration

 

for($i=0; i < $arrayLength; $i++){

 

 

if(in_array($testItemsArray[ $i ], $availableItemsArray) // grabs each item to be compared one by one .. and looks if it is in the array of available items

{

echo "images/" . $testItem[ $i ] . ".png";

}else{

echo "Item doesn't exist";

}

 

}

 

 

You might want to put a strtolower() in there somewhere to deal with capitalization discrepencies

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.