TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   3 Dimensional Array (http://www.talkphp.com/advanced-php-programming/1820-3-dimensional-array.html)

ReSpawN 12-30-2007 07:18 PM

3 Dimensional Array
 
Hey guys,

I've been trying to figure out something for quite some time now. Decided to give it a try.

PHP Code:

    $result query('SELECT * FROM user_data WHERE user_id = "2"');
    
$b03tz mysql_fetch_array($result);

    
$result2 query('SELECT * FROM user_data WHERE user_id = "3"');
    
$respawn mysql_fetch_array($result2);
    
    
$globals = array();
    foreach (
$respawn as $key => $value) {
        
array_push($globals$respawn2[$key]);
    }
    foreach (
$b03tz as $key => $value) {
        
array_push($globals$b03tz[$key]);
    } 

In plain man terms, it's shows a list with some data.

What do I want to do?
We have 2 arrays in an array. The one exports ReSpawN's stats, and the other exports b03tz's. In $global, I've pushed $respawn row results and $b03tz row results.

Now the question is, how do I make them show up in a list like so:
echo $global[$respawn[1]]; or something like that. It's completely new to me so I haven't got a clue what to do.

I also want to GENERATE the difference. When my level of, lets say, a building, is higher than that of b03tz, it shows the difference. For example:

ReSpawN 1
b03tz 0
Difference 1
Mine of course, is green, and b03tz's is red.

Can one of you guys help me out here? :-)

Mark

Alan @ CIT 12-30-2007 08:48 PM

Hi Mark,

I'm afraid I don't fully understand what you are trying to do, but I've taken a guess based on your thread title :D

At the moment, your current code would make your $globals array look something like this:

PHP Code:

/*
Array
(
    [0] => respawn_value1
    [1] => respawn_value2
    [2] => respawn_value3
    [3] => b03tz_value1
    [4] => b03tz_value2
    [5] => b03tz_value3
)
*/ 

Which I'm guessing is not what you where after. I've made some adjustments to the code to put your 2 arrays into a 3 dimensional array:

PHP Code:

$b03tz = array('b03tz1' => 'b03tz_value1',
               
'b03tz2' => 'b03tz_value2',
               
'b03tz3' => 'b03tz_value3');

$respawn = array('respawn1' => 'respawn_value1',
               
'respawn2' => 'respawn_value2',
               
'respawn3' => 'respawn_value3');    

           
$globals = array(); 

foreach (
$b03tz as $key => $value)
{
    
$globals['b03tz'][$key] = $value;
}

foreach (
$respawn as $key => $value)
{
    
$globals['respawn'][$key] = $value;


This new code makes your $globals array look like this:

PHP Code:

/*
Array
(
    [b03tz] => Array
        (
            [b03tz1] => b03tz_value1
            [b03tz2] => b03tz_value2
            [b03tz3] => b03tz_value3
        )

    [respawn] => Array
        (
            [respawn1] => respawn_value1
            [respawn2] => respawn_value2
            [respawn3] => respawn_value3
        )

)
*/ 

Is this the sort of array that you where trying to achieve?

To access the values in this array you would use something like:

PHP Code:

echo $globals['respawn']['respawn2'];

// This would echo "respawn_value2" if using the above arrays 

As for the comparison question, if you could give some examples of the data in the arrays I can probably knock some examples up for you.

Alan

Nor 01-07-2008 05:12 PM

Then again ^ thats just a two dimentional array. To be honest only reason I could see a 3 dimentional array being used is because you are doing something with levels and maps ie:

$game['level_1']['map1']['map_peice1'] = 1;
$game['level_1']['map1']['map_peice2'] = 1;

So maybe 3 dimentional arrays are not what he means possibly what you said is what hes looking for :).


All times are GMT. The time now is 11:18 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0