Title: Need PHP help for sorting an array
Last modified: August 19, 2016

---

# Need PHP help for sorting an array

 *  Resolved [alexleonard](https://wordpress.org/support/users/alexleonard/)
 * (@alexleonard)
 * [18 years ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/)
 * As I’m not very strong on PHP, I thought I’d ask here for some help.
 * I’m trying to sort out a way of displaying my links from one category, but in
   three columns. As this list is going to be expanding I need to code this in a
   future proof way.
 * I’m thinking that the best way to do this would be to use the get_bookmarks function
   to return an array of my bookmarks. I could then analyse the array, work out 
   how many bookmarks are in it, divide by 3, and then display that number of bookmarks
   in each column, being careful to avoid any duplication.
 * Problem is I haven’t the first notion of how I would go about doing this!
 * Here’s my english/code demo:
 *     ```
       <?php
       $array = get_bookmarks('category=1');
       $columnlength = $array/3;
       ?>
   
       <div class="col1">
       wp_list_bookmarks('limit=$columnlength');
       </div>
   
       <!-- As there's no offset function so I don't know what to do for columns 2 and 3 -->
       <div class="col2">
       wp_list_bookmarks('limit=$columnlength');
       </div>
   
       <div class="col3">
       wp_list_bookmarks('limit=$columnlength');
       </div>
       ```
   
 * I am assuming that I’m going to need to do something a little more sophisticated
   and outside of the get_bookmarks/wp_list_bookmarks functions..?

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/page/2/?output_format=md)

 *  [haochi](https://wordpress.org/support/users/haochi/)
 * (@haochi)
 * [18 years ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-742588)
 * Well, you can just pull out all the bookmarks in category 1 using just wp_list_bookmarks(
   set [echo=0](http://codex.wordpress.org/wp_list_bookmarks#Related)) and separate
   them into three pieces.
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-742593)
 * The idea of a limit incorporates an offset into it, in this particular case. 
   Because it’s using mySql’s LIMIT function, limit can actually be LIMIT offset,
   limit.
 *     ```
       php
       $array = get_bookmarks('category=1');
       $columnlength = (int)count($array)/3;
       ?>
       <div class="col1"><?php wp_list_bookmarks(array(
       'limit'=>'0,'.$columnlength,
       'category'=>1,
       ) ); ?></div>
   
       <div class="col2"><?php wp_list_bookmarks(array(
       'limit'=>$columnlength.','.$columnlength,
       'category'=>1,
       ) ); ?></div>
   
       <div class="col3"><?php wp_list_bookmarks(array(
       'limit'=>$columnlength*2.','.$columnlength+2,
       'category'=>1,
       ) ); ?></div>
       ```
   
 * Might work. You might need to throw some +1’s in there, I didn’t think the logic
   entirely through.
 *  [freekill](https://wordpress.org/support/users/freekill/)
 * (@freekill)
 * [18 years ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-742599)
 * Could you do it entirely with HTML and CSS? Wrap each bookmark in a floating 
   div with 33% width, and your bookmarks should start to form 3 columns on their
   own…
 * something like:
 *     ```
       <div id="bookmarks">
            <div style="width: 30%; float: left" class="float-mark">Bookmark</div>
            ... (continued for all bookmarks)
        </div>
       ```
   
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-742601)
 * Floating all the bookmarks left would work, the problem is then they would not
   order up vertically but horizontally. Not a problem if you don’t care about the
   order though.
 *  Thread Starter [alexleonard](https://wordpress.org/support/users/alexleonard/)
 * (@alexleonard)
 * [18 years ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-742739)
 * Wow. Thanks everyone for the quick responses there 🙂
 * I’m going these out this morning and see what sorts it out. I’ll report back 
   here later 🙂
 * [@freekill](https://wordpress.org/support/users/freekill/) Thanks for the suggestion,
   but Otto42 had it right about them needing to order vertically, and what you 
   suggest could probably be achieved in a less html code intensive way with:
 *     ```
       <ul id="bookmarks">
       <li>bookmark 1</li>
       <li>bookmark 2</li>
       <!-- etc -->
       </ul>
       ```
   
 * With a CSS rules of:
 *     ```
       #bookmarks {
         width: 500px; /* or relevant width */
       }
   
       #bookmarks li {
         float: left;
         width: 30%;
         padding-right: 3%;
       }
       ```
   
 * Potentially you probably already knew this!
 *  Thread Starter [alexleonard](https://wordpress.org/support/users/alexleonard/)
 * (@alexleonard)
 * [18 years ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-742759)
 * Ok. I’m encountering some trouble, but I’m assuming it’s my lack of basic PHP
   knowledge here!
 * I have a few different links pages around the site in question, and as a result
   have created one links-page template which is called for each of these pages.
   Within the links page template I’m stating if is_page 22, display links in this
   manner, if is_page 45, display this way, etc.
 * So I’ve decided that I only need to go with two columns, as 3 cols was two crowded.
   I’m using this variation of the code Otto42 put up, and while the page in question
   loads, the only code that’s rendering is my div’s – so there’s an issue with 
   the wp_list_bookmarks..
 *     ```
       // previous if statements for other pages
   
       } elseif (is_page('67')) {
         $array = get_bookmarks('category=12');
         $columnlength = (int)count($array)/2;
         ?>
         <div class="support-col1">
          <?php wp_list_bookmarks(array( 'limit'=>'0,'.$columnlength, 'category'=>12, ) ); ?>
         </div>
         <div class="support-col2">
          <?php wp_list_bookmarks(array( 'limit'=>$columnlength.','.$columnlength+1, 'category'=>12, ) ); ?>
         </div>
         <?php
       	} else { // final fallback for other links pages }; ?>
       ```
   
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-742785)
 * Hmmm.. That looks like it should work to me.
 * Try setting ‘categorize’=>0 in the list bookmarks call.
 *  Thread Starter [alexleonard](https://wordpress.org/support/users/alexleonard/)
 * (@alexleonard)
 * [18 years ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-742845)
 * Thanks for sticking with me on this one! Still no joy with categorize. Here’s
   the full code that I’m using, in case something else is throwing things out. 
   I think the else statements are fine as it’s sending out the <div class=”support-
   col1″> but nothing in between..
 *     ```
       <?php if (is_page('53'))  {
       $cats = get_categories("type=link&hierarchical=0&include=4");
        foreach ($cats as $cat) {
         echo '<h2>'.$cat->cat_name.'</h2>';
         echo '<ul>';
         $books = get_bookmarks("category=$cat->cat_ID");
         foreach ($books as $book) {
          echo '<li><p><a href="'.$book->link_url.'">'.$book->link_name.'</a></p>';
          echo '<p>'.$book->link_description.'</p>';
          echo '</li>';
         } // end books loop
        echo '</ul>';
        } // end cats loop;
        } elseif (is_page('67')) {
         $array = get_bookmarks('category=12');
         $columnlength = (int)count($array)/2;
         ?>
         <div class="support-col1">
         <?php wp_list_bookmarks(array( 'limit'=>'0,'.$columnlength, 'category'=>12,'categorize'=>0 ) ); ?>
         </div>
         <div class="support-col2">
         <?php wp_list_bookmarks(array( 'limit'=>$columnlength.','.$columnlength+1, 'category'=>12,'categorize'=>0 ) ); ?>
         </div>
         <?php
        } else {
         $cats = get_categories("type=link&hierarchical=0&exclude=4,12");
         foreach ($cats as $cat) {
         echo '<h2>'.$cat->cat_name.'</h2>';
         echo '<ul>';
         $books = get_bookmarks("category=$cat->cat_ID");
         foreach ($books as $book) {
         echo '<li><p><a href="'.$book->link_url.'">'.$book->link_name.'</a></p>';
         echo '<p>'.$book->link_description.'</p>';
         echo '</li>';
        } // end books loop
        echo '</ul>';
        } // end cats loop;
       };?>
       ```
   
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-742847)
 * This line is a bit confusing…
    `$columnlength = (int)count($array)/2;`
 * Try this:
    `$columnlength = (int)((count($array))/2);`
 * Ensure that it’s an int and not some kind of float or something.
 *  Thread Starter [alexleonard](https://wordpress.org/support/users/alexleonard/)
 * (@alexleonard)
 * [18 years ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-742850)
 * Ok. That partially worked! It’s now displaying two unordered lists with half 
   the number of bookmarks listed!
 * Only problem that remains… it’s showing the same items in each list! So it’s 
   sussed out that there are 68 bookmarks in that category, and is displaying the
   first 34 from each list.
 * Thanks for sticking at this for me (I think you’re going to deserve a beer at
   the end of this 😉
 *  Thread Starter [alexleonard](https://wordpress.org/support/users/alexleonard/)
 * (@alexleonard)
 * [18 years ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-742888)
 * I had a tinker and got it working.. I realised that I was too close and it had
   to be something simple. Turns out that I had a +1 in there that wasn’t necessary…
 * The correct code has ended up at:
 *     ```
       <?php
       $array = get_bookmarks('category=12');
       $columnlength = (int)((count($array))/2);
       ?>
       <div class="support-col1">
        <?php wp_list_bookmarks(array( 'limit'=>'0,'.$columnlength, 'category'=>12,'categorize'=>0 ) ); ?>
       </div>
       <div class="support-col2">
        <?php wp_list_bookmarks(array( 'limit'=>$columnlength.','.$columnlength, 'category'=>12,'categorize'=>0 ) ); ?>
       </div>
       ```
   
 * Thanks for the help Otto! Once the site is live I’ll send on a wee link so you
   can see what I was doing 🙂
 *  Thread Starter [alexleonard](https://wordpress.org/support/users/alexleonard/)
 * (@alexleonard)
 * [18 years ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-742892)
 * And a wee beer should be flying towards you too 🙂
 *  [bcreighton](https://wordpress.org/support/users/bcreighton/)
 * (@bcreighton)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-742973)
 * I used this my three columns – and I have just one quick problem. How do I get
   rid of the h2 header? ~thanks
 *  [bcreighton](https://wordpress.org/support/users/bcreighton/)
 * (@bcreighton)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-742974)
 * Ooo, got it! For those who need it:
 * `<?php wp_list_bookmarks(array( 'limit'=>'0,'.$columnlength, 'categorize'=>0,'
   title_li' =>0) ); ?>`
 * Thanks for everyone’s hard work on this thread! Hopefully WP will add an offset
   parameter to make this a wee easier.
 *  [peterfrom](https://wordpress.org/support/users/peterfrom/)
 * (@peterfrom)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/#post-743034)
 * Great reading… One question, though… How would I achieve the same thing but with
   the category loop intact? I need to show the list in exactly the same way as 
   above but with the categories on top of the links.
 * That is, I need to fetch all categories and all links (bookmarks) and then list
   them in three columns. If I change the categorize to 1, then all the links (bookmarks)
   show up in the first column only but with the categories on top of the links.
   Using categorize with the value zero gives me the columns but with no category
   names.
 * Help would be appreciated.

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/page/2/?output_format=md)

The topic ‘Need PHP help for sorting an array’ is closed to new replies.

## Tags

 * [bookmarks](https://wordpress.org/support/topic-tag/bookmarks/)
 * [columns](https://wordpress.org/support/topic-tag/columns/)
 * [lists](https://wordpress.org/support/topic-tag/lists/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 17 replies
 * 8 participants
 * Last reply from: [jimmiejo](https://wordpress.org/support/users/jimmiejo/)
 * Last activity: [17 years, 6 months ago](https://wordpress.org/support/topic/need-php-help-for-sorting-an-array/page/2/#post-743055)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
