Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post

    How to create empty 3 dimensional array & fill it with nested for loops?

    I'm trying to create an empty 3 dimensional array, then use 3 nested for loops to fill the array. For now I'm doing this just to learn how to do it.

    The code below tries to use two nested for loops. One loop for each dimension. It isn't written right. Even though I want to learn to do three nested loops, so I can create an empty three dimensional array and fill it using loops like the ones below, the code below has two to make it simpler.


    Once I know what I'm doing I'll put a third one in. When the commented out line is uncommented, the outer loop creates the first dimension just fine. When I try to use the 2nd loop to add a 2nd dimension, it doesn't work.

    Here is the error I got running the code:
    Code:
    Cannot use a sclar value as an array - line 14
    Line 14:
    $current[$k][$w] = $w;
    Here is the code:
    PHP Code:
    <?php
    if(isset($_POST['user_id']))
    {
          if(isset(
    $_POST['password']))
          {
              
    $current = array(array());
              
    $days_in_month date('t');
              for(
    $k 1$k <= $days_in_month$k++)
              {
                   
    $current[$k] = $k;
                   
    //echo "<p>$current[$k]</p>";
                   
    for($w 1$w <= $days_in_month$w++)
                   {
                       
    $current[$k][$w] = $w;
                       echo 
    "<p>$current[$k][$w]</p>";
                        
                   }
              }
          }
    }

    ?>
    Does anyone know what the error means and how to create a 3 dimensional empty array and fill it using for loops like the ones above?

    Thank you

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yeah, the error means that you cannot use a scalar value as an array.
    $current[$k] = $k; sets offset $k as that of $k, which is a scalar value. You therefore cannot dereference that located at $current[$k][$w] and attempt to assign data to it. If its to be an array, specify it as such in $current[$k] = array();.

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    First, what you're doing is making a two dimensional array, not 3. Each set of brackets lets you traverse one dimension... two brackets = two dimensions.

    PHP Code:
     $current = array(array()); 
    First, there's no need for that. Make it

    PHP Code:
     $current = array(); 
    I know your intention was to make an an array within an array, but in reality what you're trying to do is make an array of arrays. Because PHP has loose datatypes, you can basically assign a non existent variable as an array and PHP will figure out you want to create an array.

    Next:

    PHP Code:
    $current[$k] = $k
    By doing this you're making $current[$k] an integer, a scalar value. Then within the next loop, you try accessing that integer, that scalar value, as an array, which you cannot do. This is causing your bug. If you set $current[3] to 3, you cannot access 3 as an array. You want to do

    PHP Code:
    $current[$k] = array(); 
    or not set it explicitly at all. As I said above, what you're trying to do is not an array within an array, but an array of arrays. And because of the loose datatypes, PHP will often figure out what you're trying to do, as long as its not contradictory (like what you did).

    Remember to always think of an array as a grid, a set of cubes. A three dimensional array is like a rubics cube, x squares in width, y squares in length, z squares in height. A two dimensional array is like a grid. a grid isn't a set of squares in a set of squares. Its a set of a set of squares.

  • #4
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks for the help. I really appreciate it. What you said about the sclar error makes perfect sense. I made some changes. The code is written to count the days in July. What I'm doing right now for practice is to learn to write this so php returns this:
    Code:
    1 1
    1 2
    1 3
    etc. to 31
    
    Then:
    2 1
    2 2
    2 3
    etc. to 31... etc...
    This is what I think you would describe as an array with another array in it. Could you show me how to do this?

    This is how I changed the code after reading your reply:
    PHP Code:
    <?php
    if(isset($_POST['user_id']))
    {
          if(isset(
    $_POST['password']))
          {
              
    $current = array();
              
    $days_in_month date('t');
              for(
    $k 1$k <= $days_in_month$k++)
              {
                   
    $current[$k] = array();
                   echo 
    "<p>$current[$k]</p>";
                   for(
    $w 1$w <= $days_in_month$w++)
                   {
                       
    //$current[$k][$w] = $w;
                       //echo "<p>$current[$k][$w]</p>";
                        
                   
    }
              }
          }
    }

    ?>

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Well, what you really want then is not the actual value there in, but the keys. If you loop the keys, you'll get what you want. But As i can't fathom why you want a 2 dimensional array of the days of the month (a cross grid of stuff happening on different sets of days?), I can't really offer more help. What is the goal/purpose?

  • #6
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    On another thread (I think it was yesterday), I learned that javascript was not working in the php for loop I put it in. The element id's in the calendar I'm building are of the format "May120128AM_string".

    Since I had a bunch of php variables jammed together with a string to make every id in a similar way within the loop, I think it broke the javascript. The id rendered fine as the source code showed, but the javascript engine didn't like it.

    The element id above is made of several different data types. Someone on that thread yesterday talked about javascript arrays handling different data types well. Php needs to generate the calendar data first, then "push" that data into a javascript array.

    This way the javascript array can be inserted as one value between the quotes that give the html elements their id's, the getElementByID function, and as one value in a few other places. To make it work, I need for loops. The first counts the days of the month. The second the hours of each day within the month, and the third stores the various element id's that are created based on the day and hour, to manipulate "that" hour in each day of the month.


    This way, instead of jamming together php variables to provide the id's and related values, a javascript 3 dimensional array does. It acts as one value.
    Right now I'm learning to create the php end of things.

    How do I loop the keys?

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    A foreach within a foreach would probably be best.

    PHP Code:
    foreach ($current as $key1 => $col1) {
    foreach (
    $col1 as $key2 => $subcol) {
    //do stuff
    }

    I don't get the issue though, and I still don't see the need for a multidimensional array. PHP doesn't directly interact with Javascript... whether you had one array or 30 arrays, Javascript doesn't care. As far as I can tell, your third dimension is an amalgamation of the first two? And as there are a set number of hours per day, it sounds more like you need two loops rather then a multidimensional array. And it still sounds like you're talking about a two dimensional array...

  • #8
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    I'm thinking about what you said. Sometimes getting to what works is a winding road. To help me understand a little better, I'll explain my current thought.

    Just assuming for the moment the array is the best tool to use ( big assumption, I know) , the first thing to do is create an empty one and fill it with the days of the month. Php doesn't know how many that is until a variable is set to that number and placed in the for loop. The for loop is a counter that can do an original iteration that counts the days of a month, so every function that occurs after it has a known number to work with - the number the for loop counted to.

    That's why I'm so focused on the for loop to start.

    Isn't a foreach loop designed to run through elements of an array that already exist? If so, wouldn't it be the best way to run through an array after it was created by the for loop that first figured out how many days are in the current month?

    Btw, thanks for talking to me. This is interesting stuff. Thanks for the foreach code.
    Last edited by milesdriven; 07-24-2012 at 11:02 PM.

  • #9
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Well, you'd use the two for loops to create the array, then the foreach to read it back. of course, you can skip the middleman and just just the two for loops, but I thought you were trying to make the array, which is why I said the foreach.

    Given you're using the array as counters, thats what loops are for. But why not make 3 arrays rather then 1 3-dimensional array?

    You may also want to look into the range function... its designed to do pretty much what you want, though not at the mutlidimensional level.

  • #10
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Keleth View Post
    I thought you were trying to make the array, which is why I said the foreach.
    I am trying to make the array. Can I use a foreach loop make an array? I thought I could only use for loops to make something, and foreach to loop through what the for loop made.

    Quote Originally Posted by Keleth View Post
    But why not make 3 arrays rather then 1 3-dimensional array?
    I need to know the day of the month ( 1st dimension), to be able to "reach into" that day to make or get the hour ( 2nd dimension), etc.

  • #11
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Yah, I think there's been some confusion.

    The foreach code would be used to retrieve the information of the array, not to create it. you'd need the for code to make it.

    That being said, you keep saying reach into, but you were saying you wanna get out data in the format 1 1, 1 2, 2 1, 2 2, etc. You're not "reaching into" anything there, just associating data.

    Now if you wanted to do something so on the 1st at 2am, you wanted to get back 'alpha', that's something a multidimensional array is perfect for. But if you just want to get back the days and times, it seems kinda pointless to do that and keep looping within loops.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •