Jump to content

Is my code right (arrays and foreach)


Cep

Recommended Posts

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_array
foreach ($map_info_array as $mapvalue) {
        $map_img_array[] = rpg_tilehtml($url, $mapvalue);
}
[/code]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

$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 :)
Link to comment
Share on other sites

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 value
foreach ($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?
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.