+ Reply to Thread
Results 1 to 3 of 3

Thread: Need help creating 3D array.

  1. #1
    Barn Regular bryceowen is on a distinguished road bryceowen's Avatar
    Join Date
    Sep 2008
    Location
    Jacksonville, FL
    Posts
    93
    Rep Power
    4

    Need help creating 3D array.

    Here's the story. Right now, I have a page that pulls data into two 2D arrays from our database based on a date input.

    Code:
    $sql="SELECT * FROM database WHERE date='".mysql_real_escape_string($date)."' ORDER BY name;";
    $result=mysql_query($sql,$link);
    while($row=mysql_fetch_row($result)){
       $result_array[]=$row;
       if(!in_array($row[1],$index_array)){
          $index_array[]=$row[1];
       }
    }
    
    I then have two loops farther down that cycles through the ENTIRE $result_array comparing the current result entry against the index entry. This isn't that bad when there's only a few hundred entries, but when I try to run it for several thousand entries, things start to bog down. What I'd like to do is create a 3D array, using the index as the leading entry.
    Like this:

    $result_array[index][row][column]

    But I don't want to duplicate the index entry. How could I check if the index entry is already in the array and, if so, it puts that $row into the corresponding spot. (I really wish I could word that better.)

    Sort of a:
    if $row[1] is in the $result_array[index], add $row to $result_array[that_index][] else create a new index entry and add $row to $result_array[new_index][].

    Let me know if I could make my request any more confusing, I'll see what I can do.

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Barn Regular bryceowen is on a distinguished road bryceowen's Avatar
    Join Date
    Sep 2008
    Location
    Jacksonville, FL
    Posts
    93
    Rep Power
    4

    I found a solution that doesn't use in_array() and, frankly, I feel dumb for not having tried it before.

    Here's my revised code:
    Code:
    while($row=mysql_fetch_row($result)){
    	$array[$row[1]][]=$row;
    }
    
    And this outputs:
    Code:
    Array
    (
        [index1] => Array
            (
                [0] => Array
                    (
                        [0] => 1
                        [1] => index1
                    )
                [1] => Array
                    (
                        [0] => 2
                        [1] => index1
                    )
            )
        [index2] => Array
            (
                [0] => Array
                    (
                        [0] => 1
                        [1] => index2
                    )
                [1] => Array
                    (
                        [0] => 2
                        [1] => index2
                    )
            )
    )
    
    Now this kinda works the way I want it, but I'd prefer an integer for the index line, rather than the actual 'index1' etc.

    I've read the manual page for arrays and I know that if you build an array without specifying an index, it'll use integers by default and I can fake it by using:
    Code:
    while($row=mysql_fetch_row($result)){
    	if(!in_array($row[1],$index_array)){
    		$index_array[]=$row[1];
    	}
    	$index=array_search($row[1],$index_array);
    	$array[$index][]=$row;
    }
    
    But I'm sure there's a cleaner way to do it.

+ Reply to Thread

Similar Threads

  1. How can I randomly order the contents of an array?
    By theChris in forum PHP Development
    Replies: 1
    Last Post: May 14th, 2008, 11:09 AM
  2. Creating Dynamic Dropdown Menu!?
    By jarvelous in forum ASP Development
    Replies: 1
    Last Post: March 20th, 2008, 01:30 PM

Bookmarks

Posting Permissions

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

SEO by vBSEO