gibigbig Posted July 31, 2011 Share Posted July 31, 2011 my array here: $array = array("4" => 'first',"4" => 'second',"5" => 'third'); echo $array['4']; will one output "second", when i want it to output ALL values with the key "4"; is there anyway to get this without a foreach? Quote Link to comment https://forums.phpfreaks.com/topic/243374-help-with-arrays/ Share on other sites More sharing options...
trq Posted July 31, 2011 Share Posted July 31, 2011 Arrays need unique keys. Quote Link to comment https://forums.phpfreaks.com/topic/243374-help-with-arrays/#findComment-1249763 Share on other sites More sharing options...
gibigbig Posted July 31, 2011 Author Share Posted July 31, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243374-help-with-arrays/#findComment-1249770 Share on other sites More sharing options...
trq Posted July 31, 2011 Share Posted July 31, 2011 Sorry but none of that made sense to me. Quote Link to comment https://forums.phpfreaks.com/topic/243374-help-with-arrays/#findComment-1249771 Share on other sites More sharing options...
gibigbig Posted July 31, 2011 Author Share Posted July 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243374-help-with-arrays/#findComment-1249774 Share on other sites More sharing options...
trq Posted July 31, 2011 Share Posted July 31, 2011 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]; } } Quote Link to comment https://forums.phpfreaks.com/topic/243374-help-with-arrays/#findComment-1249775 Share on other sites More sharing options...
gibigbig Posted July 31, 2011 Author Share Posted July 31, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243374-help-with-arrays/#findComment-1249776 Share on other sites More sharing options...
wildteen88 Posted July 31, 2011 Share Posted July 31, 2011 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]; } Quote Link to comment https://forums.phpfreaks.com/topic/243374-help-with-arrays/#findComment-1249821 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.