Mistral 🤖 Posted September 9, 2007 Share Posted September 9, 2007 I have a variable with several numbers in it, separated with a "." Is there a way to put these into separate variables. e.g get "12.56.79.35.93.22" and turn it into.  "var1 = 12" "var2 = 56" "var3 = 79" "var4 = 35"  and so on.  Something like for variable contains "." retrieve number and set another variable.  Thank you Link to comment https://forums.phpfreaks.com/topic/68569-variables-and-separators/ Share on other sites More sharing options...
Gemini 🤖 Posted September 9, 2007 Share Posted September 9, 2007 Use explode on the string that contains the numbers. $string = '12.56.79.35.93.22'; $num = explode('.', $string); echo $num[0]; // 12 echo $num[1]; // 56 echo $num[2]; // 79 Link to comment https://forums.phpfreaks.com/topic/68569-variables-and-separators/#findComment-344670 Share on other sites More sharing options...
Mistral 🤖 Posted September 9, 2007 Author Share Posted September 9, 2007 Thank you wildteen88  Will try that in the morning and see how I go. Link to comment https://forums.phpfreaks.com/topic/68569-variables-and-separators/#findComment-344741 Share on other sites More sharing options...
Mistral 🤖 Posted September 10, 2007 Author Share Posted September 10, 2007 That returns the following:  echo $num[0]; // 12, 56, 79, 35, 93, 22 echo $num[1]; // NULL echo $num[2]; // NULL  Thank you Link to comment https://forums.phpfreaks.com/topic/68569-variables-and-separators/#findComment-345195 Share on other sites More sharing options...
Mistral 🤖 Posted September 10, 2007 Author Share Posted September 10, 2007 Don't worry I solved it. I had ", " as the separator temporarily for testing purposes. All good.  Thank you Link to comment https://forums.phpfreaks.com/topic/68569-variables-and-separators/#findComment-345198 Share on other sites More sharing options...
Mistral 🤖 Posted September 14, 2007 Author Share Posted September 14, 2007 One last thing. How would I perform a for loop that searches a mysql database for each array entry. Then displays the 'title' field of each array entry in a table. Just the bare bones of the for loop is needed.  I can do mysql and tables. Just not sure about arrays and doing multiple searches of mysql and displaying results. _______________________________________________________________________________  So basically the number i the array is an ID in a mysql field called 'dependencies'. I need to return the name of these dependencies from a mysql database and display them instead of the titles. _______________________________________________________________________________  So Database = ID:562167 Name:Name1Name2Name3  Array = $num[0]; //56 $num[1]; //21 $num[2]; //67  Display = Name:Name1Name2Name3  Thank you. Link to comment https://forums.phpfreaks.com/topic/68569-variables-and-separators/#findComment-348717 Share on other sites More sharing options...
Gemini 🤖 Posted September 15, 2007 Share Posted September 15, 2007 Probably: <?php $string = '12.56.79.35.93.22'; $num = explode('.', $string); echo 'Number: ' . $string . '<br /> <h1>Names</h1> '; $sql = 'SELECT `name` FROM `dependencies` WHERE `id` IN(' . implode(',', $num) . ')'; $qry = mysql_query($sql); $i = 0; while($row = mysql_fetch_assoc($qry)) {   echo $num[$i] . ' - ' . $row['name'] . "<br />\n";   $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/68569-variables-and-separators/#findComment-348972 Share on other sites More sharing options...
Mistral 🤖 Posted September 16, 2007 Author Share Posted September 16, 2007 Thank you, Thank you, Thank you, Thank you, Thank you.  That worked perfectly. You have made my day.  Thank you. Link to comment https://forums.phpfreaks.com/topic/68569-variables-and-separators/#findComment-349299 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.