Cep Posted March 29, 2006 Share Posted March 29, 2006 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[code]// 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_arrayforeach ($map_info_array as $mapvalue) { $map_img_array[] = rpg_tilehtml($url, $mapvalue);}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6100-is-my-code-right-arrays-and-foreach/ Share on other sites More sharing options...
ober Posted March 29, 2006 Share Posted March 29, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/6100-is-my-code-right-arrays-and-foreach/#findComment-21956 Share on other sites More sharing options...
Cep Posted March 29, 2006 Author Share Posted March 29, 2006 $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 :) Quote Link to comment https://forums.phpfreaks.com/topic/6100-is-my-code-right-arrays-and-foreach/#findComment-21958 Share on other sites More sharing options...
Cep Posted March 29, 2006 Author Share Posted March 29, 2006 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,[code]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;}[/code]I call this function when setting a new array,[code]$e_tile = rpg_gettile($eid);$s_tile = rpg_gettile($sid);[/code]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.[code]// 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 valueforeach ($map_info_array as $mapvalue ) { $map_image_array[] = rpg_tilehtml($url, $mapvalue);}[/code]The function that is being called in the foreach loop returns a string for each new key in the $map_image_array.[code]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;}[/code]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? Quote Link to comment https://forums.phpfreaks.com/topic/6100-is-my-code-right-arrays-and-foreach/#findComment-22092 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.