Jump to content

help getting the values from fopen using explode


kalster

Recommended Posts

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];
?> 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

<?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>";
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.