Revolutsio Posted January 24, 2020 Share Posted January 24, 2020 I have five names in an array and also numbers 5,2,1,3 and what i want to do is php to look at the numbers and print out the name that corresponds to the number. so for my example the name it should echo out is 'Lee' $names = array ("Stan", "John", "Dean", "Sam", "Lee"); here are the five names. The idea is to learn php with making a little fixture list with these five names. The problem i have is to make php read the number and then look up the array for the name that corresponds to the number. I hope this explains what I would like to do? Quote Link to comment https://forums.phpfreaks.com/topic/309928-pick-a-name-from-a-number/ Share on other sites More sharing options...
cyberRobot Posted January 24, 2020 Share Posted January 24, 2020 (edited) So basically, you are looking to loop through an array of numbers. For each number, you want to display the name that corresponds with that number. So in your loop, when you get to 5, it should display "Lee". And 2 should be "John". Correct? If so, where are you stuck? What have you tried so far? Be aware that PHP arrays don't start with 1 for the index, by default. Edited January 24, 2020 by cyberRobot fixed typo Quote Link to comment https://forums.phpfreaks.com/topic/309928-pick-a-name-from-a-number/#findComment-1573782 Share on other sites More sharing options...
Revolutsio Posted January 24, 2020 Author Share Posted January 24, 2020 $num1 = (5); $num2 = (2); $num3 = (1); $num4 = (3); echo "<br />"; if ($num1 = 5 echo $names[4]); i have tried this but get an error unexpedted echo Quote Link to comment https://forums.phpfreaks.com/topic/309928-pick-a-name-from-a-number/#findComment-1573783 Share on other sites More sharing options...
cyberRobot Posted January 24, 2020 Share Posted January 24, 2020 There's a syntax error in your if statement. The following manual page provides examples on writing if statements:https://www.php.net/manual/en/control-structures.elseif.php It might also help to review the following page when it comes to the comparison operator:https://www.php.net/manual/en/language.operators.comparison.php Quote Link to comment https://forums.phpfreaks.com/topic/309928-pick-a-name-from-a-number/#findComment-1573784 Share on other sites More sharing options...
cyberRobot Posted January 24, 2020 Share Posted January 24, 2020 (edited) Also, can I ask what's going on here: $num1 = (5); Is there a reason for the parentheses? Apparently PHP doesn't care if they are there. PHP appears to ignore them. Unless there is a reason for using the parentheses, it would be clearer to use the following: $num1 = 5; Edited January 24, 2020 by cyberRobot fixed typo Quote Link to comment https://forums.phpfreaks.com/topic/309928-pick-a-name-from-a-number/#findComment-1573785 Share on other sites More sharing options...
Revolutsio Posted January 24, 2020 Author Share Posted January 24, 2020 oh just copyed the name array Quote Link to comment https://forums.phpfreaks.com/topic/309928-pick-a-name-from-a-number/#findComment-1573786 Share on other sites More sharing options...
Barand Posted January 24, 2020 Share Posted January 24, 2020 (edited) If you define your array like this, to begin the indexing at 1 instead of 0, $names = array (1 => "Stan", "John", "Dean", "Sam", "Lee"); then your indexes will align with your $num values, 1 - 5; In other words $num = 5; echo $names[$num]; //--> Lee That will save you doing what ginerjm is about to tell you to do Edited January 24, 2020 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/309928-pick-a-name-from-a-number/#findComment-1573787 Share on other sites More sharing options...
ginerjm Posted January 24, 2020 Share Posted January 24, 2020 If your numbers are going to be 1-5 then you might try this: // assume that $i is the number 3 $idx = $i-1 echo "Name is: ".$name[$idx]; Quote Link to comment https://forums.phpfreaks.com/topic/309928-pick-a-name-from-a-number/#findComment-1573788 Share on other sites More sharing options...
Revolutsio Posted January 24, 2020 Author Share Posted January 24, 2020 35 minutes ago, Barand said: If you define your array like this, to begin the indexing at 1 instead of 0, $names = array (1 => "Stan", "John", "Dean", "Sam", "Lee"); then your indexes will align with your $num values, 1 - 5; In other words $num = 5; echo $names[$num]; //--> Lee Once again @Barand you come to me rescue thanks again Quote Link to comment https://forums.phpfreaks.com/topic/309928-pick-a-name-from-a-number/#findComment-1573793 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.