Kryllster Posted January 18, 2011 Share Posted January 18, 2011 Ok I have a multidimensional array and what I want to know is how to use it. I need to pick the items from the array but only the items specified or individual keys in the array how would I go about doing that. Here is the array: <?php $monstors = array ( "Skeleton"=>array ( "mob_name"=>"Skeleton", "mob_attack"=>"5", "mob_hp"=>"30", "mob_level"=>"1" ), "Skeleton Fighter"=>array ( "mob_name"=>"Skeleton Fighter", "mob_attack"=>"10", "mob_hp"=>"35", "mob_level"=>"2" ), "Skeleton Warrior"=>array ( "mob_name"=>"Skeleton Warrior", "mob_attack"=>"15", "mob_hp"=>"40", "mob_level"=>"3" ) ); ?> How would I like print_r the element in the array called Skeleton Warrior? Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/ Share on other sites More sharing options...
Pikachu2000 Posted January 18, 2011 Share Posted January 18, 2011 print_r($monstors['Skeleton Warrior']); Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161565 Share on other sites More sharing options...
Kryllster Posted January 18, 2011 Author Share Posted January 18, 2011 Can I compare an item in an array with a $_GET[''] variable and then use that item from the array in my page? Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161609 Share on other sites More sharing options...
Pikachu2000 Posted January 18, 2011 Share Posted January 18, 2011 Do what, now? Not sure what you're asking, really . . . Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161611 Share on other sites More sharing options...
Kryllster Posted January 18, 2011 Author Share Posted January 18, 2011 Ok I'll try to explain what I'm trying to do if I can. Say I have a link in a web page: http://index.php?p=skeleton Can I compare the variable of skeleton with the entry in the array and pull all the info from it to use in a page I am creating. I hope that helps I am working on it myself but haven't found the solution yet even if there is one. Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161630 Share on other sites More sharing options...
parino_esquilado Posted January 19, 2011 Share Posted January 19, 2011 if(isset($_GET['p'])){ if(isset($monstors[$_GET['p'])){ echo "monstor exists"; } } Tbh I would look into using objects in php, arrays are rather messy when you start making them multi-dimensional unnecessarily. Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161632 Share on other sites More sharing options...
Pikachu2000 Posted January 19, 2011 Share Posted January 19, 2011 I'm pretty sure you mean something like this? Just be aware that the values of p= and the array key are case-sensitive. <?php if(array_key_exists($_GET['p'], $monstors) ) { echo '<pre>'; print_r($monstors[$_GET['p']]); echo '</pre>'; } ?> Using the array you defined in the OP, with test.php?p=Skeleton, this returns: [pre] Array ( [mob_name] => Skeleton [mob_attack] => 5 [mob_hp] => 30 [mob_level] => 1 ) [/pre] Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161634 Share on other sites More sharing options...
Kryllster Posted January 19, 2011 Author Share Posted January 19, 2011 Funny you should mention that because I have some code that I am learning about objects. <?php class mob { // Monstors public $mob_name; public $attack; public $mob_hp; public $mob_level; // construct provided by phpfreaks forum function __construct(array $items) { foreach($items as $k => $v) { $this->{$k} = $v; } } function set_mob_name($mob_name) { $this->mob_name = $mob_name; } function get_mob_name() { return $this->mob_name; } function set_attack($attack) { $this->attack = $attack; } function get_attack() { return $this->attack; } function set_mob_hp($mob_hp) { $this->mob_hp = $mob_hp; } function get_mob_hp() { return $this->mob_hp; } function set_mob_level($mob_level) { $this->mob_level = $mob_level; } function get_mob_level() { return $this->mob_level; } } ?> The problem is I have no Idea how to implement it it works with an array if I print it out but I wanted something for a fighting script for a game I'm making. I wanted to have a 1 fight script fits all type of thing. I have a database but don't know how to use it with the piece of code I posted. Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161636 Share on other sites More sharing options...
parino_esquilado Posted January 19, 2011 Share Posted January 19, 2011 OMG there's an array_key_exists function . I love you Pikachu lol Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161637 Share on other sites More sharing options...
Pikachu2000 Posted January 19, 2011 Share Posted January 19, 2011 I hate it when the second sentence out of someone's mouth is 'I love you' . . . You can't love me; you barely even know me! Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161638 Share on other sites More sharing options...
parino_esquilado Posted January 19, 2011 Share Posted January 19, 2011 Funny you should mention that because I have some code that I am learning about objects. <?php class mob { // Monstors public $mob_name; public $attack; public $mob_hp; public $mob_level; // construct provided by phpfreaks forum function __construct(array $items) { foreach($items as $k => $v) { $this->{$k} = $v; } } function set_mob_name($mob_name) { $this->mob_name = $mob_name; } function get_mob_name() { return $this->mob_name; } function set_attack($attack) { $this->attack = $attack; } function get_attack() { return $this->attack; } function set_mob_hp($mob_hp) { $this->mob_hp = $mob_hp; } function get_mob_hp() { return $this->mob_hp; } function set_mob_level($mob_level) { $this->mob_level = $mob_level; } function get_mob_level() { return $this->mob_level; } } ?> The problem is I have no Idea how to implement it it works with an array if I print it out but I wanted something for a fighting script for a game I'm making. I wanted to have a 1 fight script fits all type of thing. I have a database but don't know how to use it with the piece of code I posted. You can print_r objects. I am making a similar game and using a similar method to that which you've demonstrated. You have a lot of unnecessary functions. Have two functions as follows, using parameters: <?php public get_stat($stat) { if(isset($this->$stat) { return $this->$stat; } } public set_stat($stat, $value) { if(isset($this->$stat) { $this->$stat = $value; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161641 Share on other sites More sharing options...
parino_esquilado Posted January 19, 2011 Share Posted January 19, 2011 I hate it when the second sentence out of someone's mouth is 'I love you' . . . You can't love me; you barely even know me! Figuratively speaking, I can love you without knowing you. It wasn't literal haha. Sorry, I tend to assume everyone is from the UK where figurative speech is common and nationally recognised . Unless you are British, in which case I apologise again!! haha Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161643 Share on other sites More sharing options...
Psycho Posted January 19, 2011 Share Posted January 19, 2011 OMG there's an array_key_exists function . I love you Pikachu lol I hate it when the second sentence out of someone's mouth is 'I love you' . . . You can't love me; you barely even know me! Not to mention the fact that all the array functions are right there in the manual plain as day. It's not like you did anything extraordinary to derserve his love. Kind of cheapens the whole "love" thing IMO. Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161646 Share on other sites More sharing options...
Pikachu2000 Posted January 19, 2011 Share Posted January 19, 2011 I hate it when the second sentence out of someone's mouth is 'I love you' . . . You can't love me; you barely even know me! Figuratively speaking, I can love you without knowing you. It wasn't literal haha. Sorry, I tend to assume everyone is from the UK where figurative speech is common and nationally recognised . Unless you are British, in which case I apologise again!! haha I guess my smiley at the end of that didn't have the intended effect, or my attempt at ironic humor just fell flat. It had to be one or the other. Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161651 Share on other sites More sharing options...
parino_esquilado Posted January 19, 2011 Share Posted January 19, 2011 I don't have time to read a manual, all of the built-in functions I come across tend to be from posts made on this forum or are just generic functions that I would expect to exist. I really appreciate when people post things that beneficial to me. In any case, the whole "love" thing was never meant to be expensive. Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161652 Share on other sites More sharing options...
parino_esquilado Posted January 19, 2011 Share Posted January 19, 2011 I hate it when the second sentence out of someone's mouth is 'I love you' . . . You can't love me; you barely even know me! Figuratively speaking, I can love you without knowing you. It wasn't literal haha. Sorry, I tend to assume everyone is from the UK where figurative speech is common and nationally recognised . Unless you are British, in which case I apologise again!! haha I guess my smiley at the end of that didn't have the intended effect, or my attempt at ironic humor just fell flat. It had to be one or the other. Haha Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161653 Share on other sites More sharing options...
Kryllster Posted January 19, 2011 Author Share Posted January 19, 2011 Funny you should mention that because I have some code that I am learning about objects. <?php class mob { // Monstors public $mob_name; public $attack; public $mob_hp; public $mob_level; // construct provided by phpfreaks forum function __construct(array $items) { foreach($items as $k => $v) { $this->{$k} = $v; } } function set_mob_name($mob_name) { $this->mob_name = $mob_name; } function get_mob_name() { return $this->mob_name; } function set_attack($attack) { $this->attack = $attack; } function get_attack() { return $this->attack; } function set_mob_hp($mob_hp) { $this->mob_hp = $mob_hp; } function get_mob_hp() { return $this->mob_hp; } function set_mob_level($mob_level) { $this->mob_level = $mob_level; } function get_mob_level() { return $this->mob_level; } } ?> The problem is I have no Idea how to implement it it works with an array if I print it out but I wanted something for a fighting script for a game I'm making. I wanted to have a 1 fight script fits all type of thing. I have a database but don't know how to use it with the piece of code I posted. You can print_r objects. I am making a similar game and using a similar method to that which you've demonstrated. You have a lot of unnecessary functions. Have two functions as follows, using parameters: <?php public get_stat($stat) { if(isset($this->$stat) { return $this->$stat; } } public set_stat($stat, $value) { if(isset($this->$stat) { $this->$stat = $value; } } ?> I don't understand what your trying to do with that code. I tried mine with a database but all I got was errors. Just trying to learn and understand. Thanks for all your help guys. Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161659 Share on other sites More sharing options...
parino_esquilado Posted January 19, 2011 Share Posted January 19, 2011 Funny you should mention that because I have some code that I am learning about objects. <?php class mob { // Monstors public $mob_name; public $attack; public $mob_hp; public $mob_level; // construct provided by phpfreaks forum function __construct(array $items) { foreach($items as $k => $v) { $this->{$k} = $v; } } function set_mob_name($mob_name) { $this->mob_name = $mob_name; } function get_mob_name() { return $this->mob_name; } function set_attack($attack) { $this->attack = $attack; } function get_attack() { return $this->attack; } function set_mob_hp($mob_hp) { $this->mob_hp = $mob_hp; } function get_mob_hp() { return $this->mob_hp; } function set_mob_level($mob_level) { $this->mob_level = $mob_level; } function get_mob_level() { return $this->mob_level; } } ?> The problem is I have no Idea how to implement it it works with an array if I print it out but I wanted something for a fighting script for a game I'm making. I wanted to have a 1 fight script fits all type of thing. I have a database but don't know how to use it with the piece of code I posted. You can print_r objects. I am making a similar game and using a similar method to that which you've demonstrated. You have a lot of unnecessary functions. Have two functions as follows, using parameters: <?php public get_stat($stat) { if(isset($this->$stat) { return $this->$stat; } } public set_stat($stat, $value) { if(isset($this->$stat) { $this->$stat = $value; } } ?> I don't understand what your trying to do with that code. I tried mine with a database but all I got was errors. Just trying to learn and understand. Thanks for all your help guys. I forgot the word 'function' ... it should read 'public function'... Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161661 Share on other sites More sharing options...
Kryllster Posted January 19, 2011 Author Share Posted January 19, 2011 How are you using that object? Is it in conjunction with a database or an array? I have the code I have but cant seem to get it to work actually all my coding at this point is in a nightmare state lol I guess I need to start all over and do some more planning. Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161695 Share on other sites More sharing options...
parino_esquilado Posted January 19, 2011 Share Posted January 19, 2011 The code I posted just replaces these functions function set_mob_name($mob_name) { $this->mob_name = $mob_name; } function get_mob_name() { return $this->mob_name; } function set_attack($attack) { $this->attack = $attack; } function get_attack() { return $this->attack; } function set_mob_hp($mob_hp) { $this->mob_hp = $mob_hp; } function get_mob_hp() { return $this->mob_hp; } function set_mob_level($mob_level) { $this->mob_level = $mob_level; } function get_mob_level() { return $this->mob_level; } So, to echo the mob_level you would call: echo $this->get_stat("mob_level"); Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161697 Share on other sites More sharing options...
Kryllster Posted January 19, 2011 Author Share Posted January 19, 2011 To continue on with this I have some code that works to a degree. I can get the right information from a database and display it correctly but the subtraction from the $mob_hp doesnt reflect the damage it just sits there with the correct value. <?php session_start(); // include object code include('objects/mob_lib.php'); // get db info from url $stat = $_GET['p']; // include the database code include('scripts/mobdbconnect.php'); //get the hp, if it exists if (!isset($_SESSION['player_hp'])) { $player_hp = 30; } else { $player_hp = $_SESSION['player_hp']; } // set monster hitpoints if (!isset($_SESSION['mob_hp'])) { $mob_hp = $mob->mob_hp; } else { $mob_hp = $_SESSION['mob_hp']; } // start the fight $first = mt_rand(1,100); //find out who goes first if ($first < 65) { $player_damage = 5; $mob_hp -= $player_damage; } else { $mob_attack = $mob->attack; $player_hp -= $mob_attack; } if ($player_hp <= 0) { header("Location:south.php?p=defeat"); exit(); } if($mob_hp <= 0) { header("Location:south.php?p=victory"); exit(); } //Gotta store them back in sessions $_SESSION['player_hp'] = $player_hp; $_SESSION['mob_hp'] = $mob_hp; include('scripts/interface.php'); ?> If you need more code let me know. I did try the other code but kept getting error about using $this in object form. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/224885-multidimensional-array-question/#findComment-1161763 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.