Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8540309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:34:05+00:00 2026-06-11T11:34:05+00:00

As my previous questions simplify I am trying to learn 3D arrays in PHP

  • 0

As my previous questions simplify I am trying to learn 3D arrays in PHP but each lesson is getting difficult to understand by me.

I have prepared code with empty 3D (layered) array but don’t know how to:

  1. populate it with data from MySQL database
  2. display it/reffer to it like: echo $result_value[x][y][z] gives
    value

.

include_once 'connect.php';

$player_id = '6';
$result_value = array();

$pullMapInfo = "SELECT x, y, z, value FROM mapinfo WHERE id='{$player_id}'";
$pullMapInfo2 = mysql_query($pullMapInfo) or die(mysql_error());

while ( $pullMapInfo3 = mysql_fetch_assoc($pullMapInfo2) ) {
    $result_value = array(
                          array(
                              array('', '', ''),
                              array('', '', ''),
                              array('', '', ''),
                          ),
                          array(
                              array('', '', ''),
                              array('', '', ''),
                              array('', '', ''),
                          ),
                          array(
                              array('', '', ''),
                              array('', '', ''),
                              array('', '', ''),
                          )
                      );
}

for($z = 1; $z <= 3; $z++){
for($x = 1; $x <= 16; $x++){
for($y = 1; $y <= 16; $y++){
    # echo database row's divided among layers (z parameter) for ex.: 1,1,1 = red (X,Y,Z = color value) - 1 width & 1 height on 1st layer contains red value
    # to get access to it like this: $result_value[x][y][z] => [value];
}
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:34:06+00:00Added an answer on June 11, 2026 at 11:34 am
    populate it with data from MySQL database
    display it/reffer to it like: echo $result_value[x][y][z] gives value
    

    The population stage is simply selecting at least x, y, z and value fields.

    Then, in a loop, you retrieve all the data. At that point you check whether the array prerequisites are present, if they aren’t you set them up, and set the value:

    <?php
        $db = mysql_connect($db_host,$db_user,$db_password) or die($error[0])
    
        $arr3d = array();
    
        /* Initialization to get a "dense" array
        $z_row = array_fill(0, $max_z, '<DEFAULT>');
        $y_row = array_fill(0, $max_y, $z_row);
        $arr3d = array_fill(0, $max_x, $y_row);
        unset($y_row, $z_row); // Do not waste memory
        */
    
        $query = 'SELECT x, y, z, value FROM ...;'   ;
    
        $handle = mysql_query($query) or die("$query: error: " . mysql_error());
    
        while($row = mysql_fetch_array($handle))
        {
            // $row is an array with x, y, z and value keys in this order
            list($x, $y, $z, $value) = $row;
    
            /* This if we had used mysql_fetch_assoc instead
            $x = $row['x'];
            $y = $row['y'];
            $z = $row['z'];
            $value = $row['value'];
            */
            if (!isset($arr3d[$x]))
                $arr3d[$x] = array();
            if (!isset($arr3d[$x][$y]))
                $arr3d[$x][$y] = array();
            $arr3d[$x][$y][$z] = $value;
        }
        mysql_free_result($handle);
    ?>
    

    In case you want a full (“dense”) array, you have to first loop x, y and z through all their values and assign a default value to array cells, to be overwritten by the SQL loop.

    In the code above, with dense patch left commented, if there is no row with x=5, y=2 and z=3, $arr3d[5][2][3] will be undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I looked previous questions but it didn't help. I have a very simple function
I have looked through as many previous questions as possible but never saw a
From my previous questions about gettext, one of the biggest benefits of using PHP's
If you have seen my previous questions, you'd already know I am a big
I guess there might be some overlapping with previous SO questions, but I could
Any suggestions on how I can simplify the php script below?This was my previous
Previous questions have asked if it is possible to turn compiled delegates into expression
I looked through previous questions to this effect, but my situation is slightly different...
This follows my previous questions on using lxml and Python. I have a question,
Based on some of my previous questions I have put together the following: var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.