Jump to content

Help with arrays


gibigbig

Recommended Posts

hmm, what im working on is a pokemon online game, and I want store the attacks in the level they will learn them, example

 

$array = array(
"4" => 'poison',
"4" => 'scratch',
"5" => 'tackle');

 

so when a pokemon levels up, I want to call on the level it just reached $array["4"] and get all the attacks it should learn at this level.

 

is there any solution

Link to comment
Share on other sites

lol ok let me explain it from a coder's pov.

 

I have this multidimensional array:

 

$array = array(
		array("4" => 'poison'),
		array("4" => 'tackle'),
		array("6" => 'sleep powder'),
		array("44" => 'giga drain'),
		array("32" => 'synthesis'),
		array("23" => 'growth'),
		array("1" => 'double edge'),
		array("5" => 'take down')
     );

 

It is for a game, and it is in the format array("level_number" => "attack_name")

You learn new attacks based on the level you are at, if i just moved up to level 4, i want to get a code that gets the new attacks i will learn from this multidimensional array like so:

 

foreach($array as $a){
$attacks[] = $a['4'];
}

print_r($attacks);

 

 

and the output is:

 

Array
(
    [0] => poison
    [1] => tackle
    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => 
    [7] => 
)

 

This code is bad because it has empty fields and I am not comfortable with the way it is made. I want to know if there is a prettier way to to code this.

Link to comment
Share on other sites

That code should generate errors because you are trying to access an index that doesn't exist. Also, numeric keys are not strings, they do not need quotes.

 

foreach ($array as $a) {
  if (isset($a[4])) {
    $attacks[] = $a[4];
  }
}

Link to comment
Share on other sites

yes it was an easy fix, i had already fixed it myself, but what i really wanted was a better way to get this output.

im  not comfortable using foreach() at all, my instincts are telling me there is a much easier and better way to do this

Link to comment
Share on other sites

You could reconstruct attack array so each attack level is holds an array of attacks. Example array structure

$array (
// level => array of attacks    
    4  => array('poison', 'tackle'),  
    6  =>  array('sleep powder'),
    44 => array('giga drain'),
    // etc
)

 

Then all you need to do to get the all the attacks for level 4 would be

  if (isset($array[4])) {
    $attacks[] = $array[4];
  }

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.