SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating 3 arrays from one

    Hi,

    I have an array that can have any number of values. I need to be able to split the array 3 ways and any remainder will get put onto the final array. Reason for this is I want to display the data in 3 columns and cannot have any more than this.

    Originally I though this would work:

    PHP Code:
    $count count($_SESSION['raw_keyword_list'])/3;
    $ar array_chunk($_SESSION['raw_keyword_list'],$count); 
    But it sometimes leaves me with a remainder which consequently the function puts into a fourth array.

    Any ideas?

  2. #2
    SitePoint Addict miggl's Avatar
    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't split it into 3 arrays (leave all in one) and instead focus on how you display it. Here is some pseudo code:

    Code:
    loop through array
        if array_index MOD 3 is 0 then display then create a new row
        display the current array item
    end loop
    loop 1 to (array_size - array_index)
        display blank cell
    end loop
    Hope this helps.
    Celebrate Liberty, Freedom, and Rights at The Constitutionalist.


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
  •