idire Posted August 5, 2008 Share Posted August 5, 2008 I receive a post data variable of say varx, I need to know that it corresponds to an array value of: $c[0][0]; vary would correspond to $c[1][0]; and varz would correspond to $c[3][0]; ETC How would I get php to know the association between the two variables without using an if statement for each variable e.g. if($_POST = var1) { echo $c[0][0]; } if($_POST = var2) { echo $c[1][0]; } if($_POST = var2) { echo $c[2][0]; } However that would be very inefficient and take a lot of code, whats a better way? I was thinking of maybe an array of variables, and the position in the array corresponding to the first number in $c[0][0]; Suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/118309-finding-variable-in-array/ Share on other sites More sharing options...
idire Posted August 5, 2008 Author Share Posted August 5, 2008 my possible solution is: <?php $skillay = array(vara, varb, varc) $i = 0; while($i < 26) { if($skillay == $_POST) { $usedvar = $c[$i][0]; } $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/118309-finding-variable-in-array/#findComment-608863 Share on other sites More sharing options...
Third_Degree Posted August 5, 2008 Share Posted August 5, 2008 you could use something like <?php $skillay = range('a','z'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/118309-finding-variable-in-array/#findComment-608881 Share on other sites More sharing options...
.josh Posted August 5, 2008 Share Posted August 5, 2008 can you work your code to have the top layer be associative instead of numerical, so you can just do $c['varx'][0] $c['vary'][0] $c['varz'][0] ? Quote Link to comment https://forums.phpfreaks.com/topic/118309-finding-variable-in-array/#findComment-608894 Share on other sites More sharing options...
idire Posted August 5, 2008 Author Share Posted August 5, 2008 ok in a different question is there any way to locate a number in an array then return the position in the array its in? Quote Link to comment https://forums.phpfreaks.com/topic/118309-finding-variable-in-array/#findComment-608909 Share on other sites More sharing options...
papaface Posted August 5, 2008 Share Posted August 5, 2008 http://uk2.php.net/manual/en/function.array-search.php Quote Link to comment https://forums.phpfreaks.com/topic/118309-finding-variable-in-array/#findComment-608912 Share on other sites More sharing options...
idire Posted August 5, 2008 Author Share Posted August 5, 2008 I cant see how that will return my position numbers? my array is currently: <?php $levels = array('83', '174', '276', '388', '512', '650', '801', '969', '1154', '1358', '1584', '1833', '2107', '2411', '2746', '3115', '3523', '3973', '4470', '5018', '5624', '6291', '7028', '7842', '8740', '9730', '10824', '12031', '13363', '14833', '16456', '18247', '20224', '22406', '24815', '27473', '30408', '33648', '37224', '41171', '45529', '50339', '55649', '61512', '67983', '75127', '83014', '91721', '101333', '111945', '123660', '136594', '150872', '166636', '184040', '203254', '224466', '247886', '273742', '302288', '333804', '368599', '407015', '449428', '496254', '547953', '605032', '668051', '737627', '814445', '899257', '992895', '1096278', '1210421', '1336443', '1475581', '1629200', '1798808', '1986068', '2192818', '2421087', '2673114', '2951373', '3258594', '3597792', '3972294', '4385776', '4842295', '5346332', '5902831', '6517253', '7195629', '7944614', '8771558', '9684577', '10692629', '11805606', '13034431'); Quote Link to comment https://forums.phpfreaks.com/topic/118309-finding-variable-in-array/#findComment-608915 Share on other sites More sharing options...
papaface Posted August 5, 2008 Share Posted August 5, 2008 Did you actually read the function info? <?php $levels = array('83', '174', '276', '388', '512', '650', '801', '969', '1154', '1358', '1584', '1833', '2107', '2411', '2746', '3115', '3523', '3973', '4470', '5018', '5624', '6291', '7028', '7842', '8740', '9730', '10824', '12031', '13363', '14833', '16456', '18247', '20224', '22406', '24815', '27473', '30408', '33648', '37224', '41171', '45529', '50339', '55649', '61512', '67983', '75127', '83014', '91721', '101333', '111945', '123660', '136594', '150872', '166636', '184040', '203254', '224466', '247886', '273742', '302288', '333804', '368599', '407015', '449428', '496254', '547953', '605032', '668051', '737627', '814445', '899257', '992895', '1096278', '1210421', '1336443', '1475581', '1629200', '1798808', '1986068', '2192818', '2421087', '2673114', '2951373', '3258594', '3597792', '3972294', '4385776', '4842295', '5346332', '5902831', '6517253', '7195629', '7944614', '8771558', '9684577', '10692629', '11805606', '13034431'); echo array_search("174",$levels); // returns 1, therefore in element 2 of the $levels array ?> Quote Link to comment https://forums.phpfreaks.com/topic/118309-finding-variable-in-array/#findComment-608922 Share on other sites More sharing options...
idire Posted August 5, 2008 Author Share Posted August 5, 2008 Thanks sorry I missed that, was reading the examples at the bottom what about if i have a number not in the array and i want to find the closest one above it e.g. i have 150 and want to find 174 Quote Link to comment https://forums.phpfreaks.com/topic/118309-finding-variable-in-array/#findComment-608927 Share on other sites More sharing options...
.josh Posted August 5, 2008 Share Posted August 5, 2008 Then sort the array and make a loop that stops when you get to the first number that's greater than it. Quote Link to comment https://forums.phpfreaks.com/topic/118309-finding-variable-in-array/#findComment-608934 Share on other sites More sharing options...
idire Posted August 5, 2008 Author Share Posted August 5, 2008 This seems to work, thanks <?php $var = 600; $i = 0; while($levels[$i] < $var) { $tempvar = $levels[$i+1]; $i++; } echo $tempvar; Quote Link to comment https://forums.phpfreaks.com/topic/118309-finding-variable-in-array/#findComment-608942 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.