Results 1 to 7 of 7

Thread: array_push / sort

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default array_push / sort

    running into a weird problem. i have an array reading from a dB and would like to add manually to this list then sort()
    but sort seems to only apply to the while loop, why? how can i fix this?
    as always any help would be appreciated, thanks.
    Code:
    $rows = array();
    array_push($rows, 'b new item');// this gets left out
    while ($row0 = mysql_fetch_array($result0)) 
    {
    	array_push($rows, $row0[1]);
    }
    sort($rows);// only sorts the items in the while loop
    foreach($rows as $r) {
    	$string .= "<li><a href='#'>" . $r . "</a></li>" . "\n";
    }
    echo $string;
    Last edited by ggalan; 10-27-2011 at 12:03 AM.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    works for me (the initial value is included, and sorted). What does var_dump($rows) (after the while loop) give you?

  3. #3
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    var dump out puts with the index's but 'b new item' should fall after items that start with 'a' but it doesnt. it lists as the last
    Code:
    { [0]=> string(11) "Alchemy" [1]=> string(12) "Alchemy P" [2]=> string(7) "Carezza" [3]=> string(11) "Eco" [4]=> string(6) "Fusion" [5]=> string(5) "Helio" [6]=> string(12) "b new fabric" }

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I've never seen a need to use array_push(). I just use this syntax (as mentioned on php.net for the array_push() description as well).

    PHP Code:
    $array[] = $newitem//add $newitem to end of $array 
    I've never had any problems like you are describing when I use this method. But I don't know about array_push(). I think it should work the same way, but I don't know.

    The only thing I can see is that maybe array_push() never adds the item because array_push() only works on an array that already has some contents. I don't know if that is true. Try using var_dump() or print_r() BEFORE the while loop as well. If it wasn't added then, then it won't be there later. I don't think the while loop is erasing it.
    Last edited by djr33; 10-26-2011 at 11:25 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    i see, that gave me the same result.
    heres my trimmed down flow
    Code:
    $query0 = "SELECT * FROM fabrics WHERE subCategoryID='$qs' AND discontinued=0 ORDER BY trim(fabricName)";
    $result0 = mysql_query($query0) or die (mysql_error());
    
    $rows[] = 'b new fabric';
    while ($row0 = mysql_fetch_array($result0)) 
    {
    	$rows[] = $row0[1];
    }
    sort($rows);
    //var_dump($rows);
    $string = "<ul class='sub-menu sub-menu-home'>" . "\n";
    foreach($rows as $r) {
    	$r = ucwords($r);
    	$string .= "<li><a href='#'>" . $r . "</a></li>" . "\n";
    	
    }
    $string .= "</ul>";
    echo $string;
    Last edited by ggalan; 10-27-2011 at 12:03 AM.

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

  7. The Following User Says Thank You to traq For This Useful Post:

    ggalan (10-26-2011)

  8. #7
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    awesome! thanks

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
  •