#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,670
    Rep Power
    171

    Displaying array elements based on array key


    Hello;

    I have an array I want to group the elements and show them based on RID key.

    I want to get this from the array:
    Code:
    <div id="187219">
        <p id="9376">9376</p>
    </div>
    
    <div id="187228">
        <p id="9377">9377</p>
        <p id="9396">9396</p>
        <p id="939 7">9397</p>
    </div>
    PHP Code:
    print_r($data); 
    Code:
    Array ( 
    
    [0] => Array ( [total] => 888 [number_of_rooms] => 4 [status] => b [PID] => 9367 [RID] => 187219 [PTYPE] => Single Night Stays [roomname] => Suit 2 )
    
    [1] => Array ( [total] => 100 [number_of_rooms] => 4 [status] => b [PID] => 9376 [RID] => 187219 [PTYPE] => Stay 2 Pay 1 [roomname] => Suit 2 )
    
    [2] => Array ( [total] => 200 [number_of_rooms] => 4 [status] => b [PID] => 9377 [RID] => 187228 [PTYPE] => Single Night Stays [roomname] => behnam )
    
    [3] => Array ( [total] => 250 [number_of_rooms] => 0 [status] => r [PID] => 9396 [RID] => 187228 [PTYPE] => Last Minute [roomname] => behnam )
    
    [4] => Array ( [total] => 300 [number_of_rooms] => 0 [status] => r [PID] => 9397 [RID] => 187228 [PTYPE] => Bed and Breakfast [roomname] => behnam) 
    )
    What would be the best way?
    Thank you
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Use a loop to create a new array which is structured in a way that is useful for outputting that HTML structure.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,670
    Rep Power
    171
    Originally Posted by E-Oreo
    Use a loop to create a new array which is structured in a way that is useful for outputting that HTML structure.
    I did that put I got a little issue.
    PHP Code:
    $groupedData = array();
    foreach(
    $data as $item){
        if(!
    array_key_exists($item['RID'],$groupedData)){
            
    $groupedData[$item['RID']] = array();
        }
            
    $groupedData[$item['RID']][] = $item;

    I want to show the array that has [PID] => 9396 first. How can I do that?
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    What is special about 9396?
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,670
    Rep Power
    171
    Originally Posted by E-Oreo
    What is special about 9396?
    Nothing. It's dynamic. It could be 5873 could be anything. I need to sort the array based on that. Something like:

    PHP Code:
    if($_GET['pid']== array's [PID] value)
         {
              //Sort array and put this element first.
         }
    print_r($array); 
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    OK, so it sounds like you have a correct idea of what you need to do ("Sort array and put this element first").

    A good place to start, as always, is the PHP manual. If you go to php.net, enter "sort array" into the search box and press search, the first result is a page titled: PHP: Sorting Arrays - Manual

    This page lists 13 different functions you can use to sort arrays. They are arranged in a table to help you decide which function to use, and at the top of the page there is a description of the differences.

    Some sort based on the array keys, whereas others by the values: $array['key'] = 'value';
    So first determine whether you are sorting based on the key or based on the value. You are sorting the $groupedData array, and the key of the $groupedData array is the RID. Since your goal is to list a specific PID value first, and because we have already established that RID is the key we know that PID cannot also be the key, then you are clearly not sorting by key and so must be sorting by value.

    Whether or not the correlation between the keys and values are maintained after the sort, which may mean the keys are reset numerically (0,1,2 ...)
    This is asking whether or not the keys of the array that you're sorting ($groupedData) have important meaning. In this case, they represent the RID which has actual meaning. Therefore, you do want to keep this value intact after the sort.

    The order of the sort: alphabetical, low to high (ascending), high to low (descending), numerical, natural, random, or user defined
    The value of PID is a number, so alphabetical makes no sense. Low-to-high, high-to-low, numerical and natural do not make sense as options in this specific case because, although your value is a number, you want to list it first because it appears in another variable ($_GET['pid']), not because it is higher or lower than any other PID. random obviously is not going to be helpful at all here. So that only leaves one option.

    Of the 13 functions listed on that page, only one actually does what you need in this situation.

    Comments on this post

    • ManiacDan agrees
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  12. #7
  13. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,670
    Rep Power
    171
    Originally Posted by E-Oreo
    OK
    Thanks for that. I think uasort is the one matching the criteria. But in the examples I can't find anything like my requirements. What do you think?
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    uasort does appear to be what you need. But note the "u" means that "you do it." You write the custom sorter function. It compares two elements and determines which one is greater than the other. Start writing it and let us know when you're stuck.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo