Solved_ Posted July 20, 2012 Share Posted July 20, 2012 Beginner here: I have a array with numbers out of a database, and they correspond with words. This is the translation I already know: 1 = bread 2 = pizza 3 = water ect... Lets's say the array contains the following numbers: $aWords = array (1, 2, 4, 5, 7, 12, 15); Now I want to check if a number is in the array, and then echo a image if it is there, so a bread image for 1, and a pizza image for 2. I used a switch, but this stopped after finding the first case. I can use a lot of if statements but is there not a easier function I can use? Quote Link to comment https://forums.phpfreaks.com/topic/265974-check-array-for-multiple-values-and-give-unique-returns/ Share on other sites More sharing options...
Pikachu2000 Posted July 20, 2012 Share Posted July 20, 2012 Why not a foreach loop? Quote Link to comment https://forums.phpfreaks.com/topic/265974-check-array-for-multiple-values-and-give-unique-returns/#findComment-1362944 Share on other sites More sharing options...
Solved_ Posted July 20, 2012 Author Share Posted July 20, 2012 Thanks. I have this code now, which is working. <?php $aWords = array(1, 2, 4, 5, 7, 12, 15); foreach($aWords as $key => $value) { echo '<img src=('.'http://example.com/imagepath/'.$value.'.jpg'.')/>'.'</br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265974-check-array-for-multiple-values-and-give-unique-returns/#findComment-1362946 Share on other sites More sharing options...
Solved_ Posted July 20, 2012 Author Share Posted July 20, 2012 One more question: Showing images is easy because I can base the matching on the numbers it self. What if I want to show the word bread for 1, and pizza for 2, ect..? Quote Link to comment https://forums.phpfreaks.com/topic/265974-check-array-for-multiple-values-and-give-unique-returns/#findComment-1362947 Share on other sites More sharing options...
xyph Posted July 20, 2012 Share Posted July 20, 2012 Build another array containing the relationship. Key = Id, value = Text name. Use $key in your foreach to reference that text name Quote Link to comment https://forums.phpfreaks.com/topic/265974-check-array-for-multiple-values-and-give-unique-returns/#findComment-1362948 Share on other sites More sharing options...
Solved_ Posted July 20, 2012 Author Share Posted July 20, 2012 I have build this array, but I have really no clue how to finish the coding. $aTranslate = array( 1 => 'bread', 2 => 'piza', 3 => 'water', 4 => 'cornflakes', 5 => 'word5', 6 => 'word6', 7 => 'word7', 8 => 'word8', 9 => 'word9', ); What does: foreach($aWords as $key => $value) the relation $aWords as $key => $value mean? If I echo $aTranslate in the foreach I get "array" repeated every time. If I change $key to $aTranslate: foreach($aWords as $aTranslate => $value) Then it counts from 0 to 5 if I use 6 values in $aWords = array(1, 2, 5, 7, 8, 9); Quote Link to comment https://forums.phpfreaks.com/topic/265974-check-array-for-multiple-values-and-give-unique-returns/#findComment-1362951 Share on other sites More sharing options...
ignace Posted July 20, 2012 Share Posted July 20, 2012 $aWords = array(1, 2, 4, 5, 7, 12, 15); $aTranslate = array( 1 => 'bread', 2 => 'piza', 3 => 'water', 4 => 'cornflakes', 5 => 'word5', 6 => 'word6', 7 => 'word7', 8 => 'word8', 9 => 'word9', ); foreach($aWords as $key => $value) { $alt = ''; if (isset($aTranslate[$key])) { $alt = $aTranslate[$key]; } echo '<img src="http://example.com/imagepath/' . $value . '.jpg" alt="' . $alt . '"><br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/265974-check-array-for-multiple-values-and-give-unique-returns/#findComment-1362961 Share on other sites More sharing options...
Solved_ Posted July 20, 2012 Author Share Posted July 20, 2012 Hi ignace, thanks for your help, I have tested your code, but it doesn't work the right way. It translates the keys of the first array, i.e: array(1, 2, 4, 5, 7, 12, 15); into the words list 1 => 'bread', 2 => 'piza', 3 => 'water', 4 => 'cornflakes', 5 => 'word5', 6 => 'word6', 7 => 'word7', 8 => 'word8', 9 => 'word9', So the first one is empty, because the key is 0, and then it translates the keys from bread until word6, not the real translation. Any solution? Quote Link to comment https://forums.phpfreaks.com/topic/265974-check-array-for-multiple-values-and-give-unique-returns/#findComment-1363184 Share on other sites More sharing options...
Pikachu2000 Posted July 20, 2012 Share Posted July 20, 2012 It's an easy fix. Take a shot at figuring it out. Quote Link to comment https://forums.phpfreaks.com/topic/265974-check-array-for-multiple-values-and-give-unique-returns/#findComment-1363186 Share on other sites More sharing options...
Solved_ Posted July 20, 2012 Author Share Posted July 20, 2012 Thanks! got it, yeah, have to try more myself if (isset($aTranslate[$value])) { $alt = $aTranslate[$value]; Quote Link to comment https://forums.phpfreaks.com/topic/265974-check-array-for-multiple-values-and-give-unique-returns/#findComment-1363188 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.