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
https://forums.phpfreaks.com/topic/243374-help-with-arrays/#findComment-1249770
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
https://forums.phpfreaks.com/topic/243374-help-with-arrays/#findComment-1249774
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
https://forums.phpfreaks.com/topic/243374-help-with-arrays/#findComment-1249821
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.