Jump to content


Photo

Is my code right (arrays and foreach)


  • Please log in to reply
3 replies to this topic

#1 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 29 March 2006 - 02:26 PM

Hello,

I created the code below. Basically I have a multi dimensional array (an array containing arrays as values). I want to run a function on each of those array values and then assign the outcome of the function to a new array.

I just don't know if what I have done is correct or not :D

// Create array to contain all tile information arrays
$map_info_array = array($current_tile, $n_tile, $e_tile, $s_tile, $w_tile, $ne_tile, $se_tile, $sw_tile, $nw_tile);

// Create empty array for image information
$map_img_array = array();

// Run function on each info array and assign value to $map_img_array
foreach ($map_info_array as $mapvalue) {
        $map_img_array[] = rpg_tilehtml($url, $mapvalue);
}

Thanks Cep

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 29 March 2006 - 02:29 PM

Well... it's kind of hard for us to tell. I don't see where you're setting $url, but then again I don't know anything about the function you're calling either. The rest looks good enough. Have you tried to run it?

Info: PHP Manual


#3 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 29 March 2006 - 02:41 PM

$url is just a string value and the function basically runs a query that creates a string and returns it.

The function as you can see takes the string value $url and one of the array values of the array $map_info_array as its arguements.

I didn't include $url string or the function because to be fair they aren't as important as knowing whether what I have done in assigning values to $map_img_array from the other array is correct.

I havent been able to run it yet because its part of a project which I am creating on the spot kind of thing :)
Thanks Cep

#4 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 29 March 2006 - 08:50 PM

Ok well I can't get this working the way I want it to.

Basically my code creates an array based on an sql query,

function rpg_gettile($tile_id) {

global $db, $n;

         $type = $db->query_first("SELECT * FROM bb".$n."_wbbrpg_location_types WHERE typeid = ".$tile_id."");

         $tile_name = $type['name'];
         $tile_img = $type['image'];
         $tile_magic = $type['magic'];
         $tile_fly = $type['fly'];
         $tile_walk = $type['walk'];
         $tile_sail = $type['sail'];
         $tile_tunnel = $type['tunnel'];

         $tile_array = array($tile_id, $tile_name, $tile_img, $tile_magic, $tile_fly, $tile_walk, $tile_sail, $tile_tunnel);

         return $tile_array;
}

I call this function when setting a new array,

$e_tile = rpg_gettile($eid);
$s_tile = rpg_gettile($sid);

I then want to add these multiple arrays to a multidimensional array so that I can run another function on each of the arrays within it.

// Create array to contain all tile information arrays
$map_info_array = array($current_tile, $n_tile, $e_tile, $s_tile, $w_tile, $ne_tile, $se_tile, $sw_tile, $nw_tile);

// Create empty array for image information
$map_image_array = array();

// Run function on each info array and assign image element string to each $map_img_array value
foreach ($map_info_array as $mapvalue ) {
        $map_image_array[] = rpg_tilehtml($url, $mapvalue);
}

The function that is being called in the foreach loop returns a string for each new key in the $map_image_array.

function rpg_tilehtml($boardurl, $tilearray) {

         $id = $tilearray[0];
         $name = $tilearray[1];
         $img = $tilearray[2];

         $tile_element = "<img src='".$boardurl.$img."' id='".$id."' alt='".$name."' title='".$name."' />";

         return $tile_element;
}

The problem is I dont think the foreach loop is working correctly because my end result is completely wrong. It is almost as if the foreach loop is not sending the individual arrays to the function rpg_tilehtml.

Can anyone help me?
Thanks Cep




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users