Results 1 to 3 of 3

Thread: How to order an array or array output

  1. #1
    Join Date
    Apr 2010

    How to order an array or array output

    I have a simple function that takes the content of an array of products and if they have amounts in them output the contents into a csv file.
    (user then clicks a link to open the csv file.)

    The array was in turn created from a Mysql DB.

    PHP Code:
    function set_create_csv(){

         if (!
    $this->handle fopen($this->filepath'w'))
    "Cannot open file (".$this->filepath.")";

    //file output header
    if (fwrite($this->handle"Product Code,Product Description,Unit Price,Qty,\n") === FALSE) {
    "Cannot write to file (".$this->filepath.")";

    $this->all_items as $key => $value){
    $description $this->all_items[$key]['description'];
    $buyprice $this->all_items[$key]['buyprice'];
    $qty $this->all_items[$key]['amount'];

    Simple and it works.

    The problem is that the output is in order of the array keys (which are actually part numbers) and not in a logical grouping that users can easily scan.

    My question is how do i order the output so that i can group parts together logically?

  2. #2
    Join Date
    Aug 2004
    My first recommendation would be to change the database query with an "order by" clause that sorts the results as desired.

    If that is impractical for some reason, then you can look at uasort() and its related functions to sort the array for you.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Apr 2010
    *($&^#(*^#( #$()^#$

    First thing I'd tried was ordering by my mysql query.

    Didnt have ANY effect. So i convinced myself that that wasnt how the foreach statement was pulling the data from the array.

    Well, of course it was how it does using the internal pointers.

    So after your post i went back and sure enough my mysql order by statement was up the duff. Corrected and works like a treatment.

    So how do i order it?

    I created a table colum called 'order' and gave each part a number that is in order of importance and grouped similar parts together.

    PHP Code:
        $query "SELECT * FROM `items` ORDER BY `items`.`Order` ASC"
    What a n00b!

    So thankyou for the tip nogdog and for not slapping me with a wet fish like a really had earnt.


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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