Jump to content

Multidimensional Array question?


Kryllster
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D 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;
    }
}

?>

 

Link to comment
Share on other sites

:shrug:

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!  :P

 

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 :D. Unless you are British, in which case I apologise again!! :D haha

Link to comment
Share on other sites

OMG there's an array_key_exists function :o . 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!  :P

 

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.

Link to comment
Share on other sites

:shrug:

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!  :P

 

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 :D. Unless you are British, in which case I apologise again!! :D 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.

Link to comment
Share on other sites

:shrug:

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!  :P

 

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 :D. Unless you are British, in which case I apologise again!! :D 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

Link to comment
Share on other sites

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 :D 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.

Link to comment
Share on other sites

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 :D 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'...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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