kalster Posted August 12, 2012 Share Posted August 12, 2012 i have a map file with the contents of "1:0,0:1". in the first value is "1" with grass and the second value is "0" for no obstacle. the third value i have mountain as "0", and the forth value i have "1" for obstacle. so "1:0,0:1" is grass, no obstacle, mountain, obstacle. i am trying to use the php command "explode" to store the map values into the variables. so that grass would be "1" and mountain would be "0" and the grass obstacle would be "0" and the mountain obstacle would be "1". here is my code that i am trying to do but with the code the obstacles are reading "1:1" for put[1]. i am trying to get the four variables in to two different arrays. your help is greatly needed here. thank you in advanced. <?php $map = fopen("map.map", "r") or die("file not found"); $file = fread($map, filesize("map.map")); $get = explode(",",$file); echo $get[0]; $put = explode(":",$file); echo "<br \>" . $put[1]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/266968-help-getting-the-values-from-fopen-using-explode/ Share on other sites More sharing options...
requinix Posted August 12, 2012 Share Posted August 12, 2012 Why are you mixing colons and commas? If there's no difference then pick one and stick with it. list($one, $two, $three, $four) = explode("whichever", file_get_contents("map.map")); Otherwise there's some underlying structure you aren't telling us. Your code should mirror it. Quote Link to comment https://forums.phpfreaks.com/topic/266968-help-getting-the-values-from-fopen-using-explode/#findComment-1368731 Share on other sites More sharing options...
kalster Posted August 12, 2012 Author Share Posted August 12, 2012 Why are you mixing colons and commas? If there's no difference then pick one and stick with it. list($one, $two, $three, $four) = explode("whichever", file_get_contents("map.map")); Otherwise there's some underlying structure you aren't telling us. Your code should mirror it. in the map of "1:0,01" of the first "1" is grass and then the colon followed by "0" is the obstacle. if it is "0" then the grass can be walked on. then the next in the map is "," followed by mountain which is "0" followed by "1" which means the mountain can't be walked on. i am mixing colons with commas because after the colons is rather it is obstacle or not and after the commas is the graphics. your code will not work because there is no way to tell rather the grass is obstacle or not. Quote Link to comment https://forums.phpfreaks.com/topic/266968-help-getting-the-values-from-fopen-using-explode/#findComment-1368735 Share on other sites More sharing options...
xyph Posted August 12, 2012 Share Posted August 12, 2012 <?php $map_data = '1:0,0:1'; // This will hold our final data $final_data = array(); // First break up by ',' $map_array = explode(',', $map_data); // Loop through results foreach( $map_array as $map_value ) { // Explode each combo by : list( $type, $obstacle ) = explode(':', $map_value); // Add a new array into our final data $final_data[$obstacle] = $type; } // Use results later in a loop foreach( $final_data as $obstacle => $type ) { echo "The obstacle numer $obstacle is of type $type<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/266968-help-getting-the-values-from-fopen-using-explode/#findComment-1368739 Share on other sites More sharing options...
kalster Posted August 12, 2012 Author Share Posted August 12, 2012 works great. thank you xyph Quote Link to comment https://forums.phpfreaks.com/topic/266968-help-getting-the-values-from-fopen-using-explode/#findComment-1368743 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.