Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

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

    Finding arrays element!


    In the array below the value of the element "total" is in red color. How can I choose the element with the smallest value for "total" for all the elements with the same hotel_id? Which in this case is 400. This is the array
    Array ( [0] => Array ( [hotel_id] => 229 [total] => 700 [number_of_rooms] => 0 [status] => r [PID] => 2088
    [HID] => 229 [PLACENAME] => Rainbow Beach [hotelname]
    => Rainbow Ocean Palms Resort [destinationname] =>
    Sunshine Coast [startype] => 5 )

    [1] => Array ( [hotel_id] => 229 [total] => 400
    [number_of_rooms] => 0 [status] => r [PID] => 2302
    [HID] => 229 [PLACENAME] => Rainbow Beach [hotelname]
    => Rainbow Ocean Palms Resort [destinationname] =>
    Sunshine Coast [startype] => 5 )

    [2] => Array ( [hotel_id] => 229 [total] => 800
    [number_of_rooms] => 0 [status] => r [PID] => 2576
    [HID] => 229 [PLACENAME] => Rainbow Beach [hotelname]
    => Rainbow Ocean Palms Resort [destinationname] =>
    Sunshine Coast [startype] => 5 ) )
    I got some help here previously but it picks the wrong element!
    PHP Code:
    $lookup = array();

    foreach (
    $hotels_selection as $key => $value)
        {
            if (isset(
    $lookup[$value["hotel_id"]]))
                {
                    if (
    $hotels_selection[$lookup[$value["hotel_id"]]][0]["total"] < $value[0]["total"])
                        {
                            
    $lookup[$value["hotel_id"]] = $value;
                        }
                }
            else
                {
                    
    $lookup[$value["hotel_id"]] = $value;
                }
        } 
    Here is the page Thank you.
    Last edited by zxcvbnm; April 2nd, 2013 at 12:19 AM.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,180
    Rep Power
    9398
    Consider using a temporary variable or two: going three array keys deep can start playing tricks on your eyes.

    Assuming that array dump is for $hotels_selection, to get the total just use $value["total"].
    As for the general concept, keep a variable for the minimum total so far and one for the array key it was found in. Then loop through and keep updating those two variables as you go. Which looks like what you have... I think?
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,675
    Rep Power
    171
    Originally Posted by requinix
    Consider using a temporary variable or two: going three array keys deep can start playing tricks on your eyes.

    Assuming that array dump is for $hotels_selection, to get the total just use $value["total"].
    As for the general concept, keep a variable for the minimum total so far and one for the array key it was found in. Then loop through and keep updating those two variables as you go. Which looks like what you have... I think?
    Not really! There is something wrong with the code above you wrote for me. $lookup should have the element with [total] => 400 not [total] => 700
    PHP Code:
    print_r($hotels_selection);/*Prints:
    [0] => Array ( [hotel_id] => 229 [total] => 700 [number_of_rooms] => 0 [status] => r [PID] => 2088 [HID] => 229 [PLACENAME] => Rainbow Beach [hotelname] => Rainbow Ocean Palms Resort [destinationname] => Sunshine Coast [startype] => 5 ) 

    [1] => Array ( [hotel_id] => 229 [total] => 400 [number_of_rooms] => 0 [status] => r [PID] => 2302 [HID] => 229 [PLACENAME] => Rainbow Beach [hotelname] => Rainbow Ocean Palms Resort [destinationname] => Sunshine Coast [startype] => 5 ) 

    [2] => Array ( [hotel_id] => 229 [total] => 800 [number_of_rooms] => 0 [status] => r [PID] => 2576 [HID] => 229 [PLACENAME] => Rainbow Beach [hotelname] => Rainbow Ocean Palms Resort [destinationname] => Sunshine Coast [startype] => 5 ) )
    */


    print_r($lookup);/*prints:
    Array ( [229] => Array ( [hotel_id] => 229 [total] => 700 [number_of_rooms] => 0 [status] => r [PID] => 2088 [HID] => 229 [PLACENAME] => Rainbow Beach [hotelname] => Rainbow Ocean Palms Resort [destinationname] => Sunshine Coast [startype] => 5 ) ) */ 
    So something should be changed here
    PHP Code:
     $lookup = array();

    foreach (
    $hotels_selection as $key => $value)
        {
            if (isset(
    $lookup[$value["hotel_id"]]))
                {
                    if (
    $hotels_selection[$lookup[$value["hotel_id"]]][0]["total"] < $value[0]["total"])
                        {
                            
    $lookup[$value["hotel_id"]] = $value;
                        }
                }
            else
                {
                    
    $lookup[$value["hotel_id"]] = $value;
                }
        } 
    Please continue the help. Thank you.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    397
    Rep Power
    189
    Try this:

    PHP Code:
    $lookup = array();
    foreach (
    $hotels_selection as $key => $value){
        if (isset(
    $lookup[$value["hotel_id"]])){
            if (
    $lookup[$value["hotel_id"]] > $value["total"]){
                
    $lookup[$value["hotel_id"]] = $value["total"];
            }
        }else{
            
    $lookup[$value["hotel_id"]] = $value["total"];
        }

    Thomas Tremain
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,675
    Rep Power
    171
    Originally Posted by ttremain
    Try this:

    PHP Code:
    $lookup = array();
    foreach (
    $hotels_selection as $key => $value){
        if (isset(
    $lookup[$value["hotel_id"]])){
            if (
    $lookup[$value["hotel_id"]] > $value["total"]){
                
    $lookup[$value["hotel_id"]] = $value["total"];
            }
        }else{
            
    $lookup[$value["hotel_id"]] = $value["total"];
        }

    Looks correct thank you. Please put the rest of the data in there as well so when I print_r($lookup); I see hotel_id and... as before.

    [0] => Array ( [hotel_id] => 229 [total] => 700 [number_of_rooms] => 0 [status] => r [PID] => 2088 [HID] => 229 [PLACENAME] => Rainbow Beach [hotelname] => Rainbow Ocean Palms Resort [destinationname] => Sunshine Coast [startype] => 5 )

    [1] => Array ( [hotel_id] => 229 [total] => 400 [number_of_rooms] => 0 [status] => r [PID] => 2302 [HID] => 229 [PLACENAME] => Rainbow Beach [hotelname] => Rainbow Ocean Palms Resort [destinationname] => Sunshine Coast [startype] => 5 )

    [2] => Array ( [hotel_id] => 229 [total] => 800 [number_of_rooms] => 0 [status] => r [PID] => 2576 [HID] => 229 [PLACENAME] => Rainbow Beach [hotelname] => Rainbow Ocean Palms Resort [destinationname] => Sunshine Coast [startype] => 5 ) )
    ------------------------------------------------

    Array ( [229] => 400 )
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    397
    Rep Power
    189
    Actually maybe this is closer to what you are looking for:

    PHP Code:
    foreach ($hotels_selection as $key => $value){
        if (isset(
    $lookup["total"])){
            if (
    $lookup["total"] > $value["total"]){
                
    $lookup $value;
            }
        }else{
            
    $lookup $value;
        }

    Thomas Tremain
  12. #7
  13. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,675
    Rep Power
    171
    Originally Posted by ttremain
    Actually maybe this is closer to what you are looking for:

    PHP Code:
    foreach ($hotels_selection as $key => $value){
        if (isset(
    $lookup["total"])){
            if (
    $lookup["total"] > $value["total"]){
                
    $lookup $value;
            }
        }else{
            
    $lookup $value;
        }

    Not really this is missing one dimention. I want my result to look like this:
    Code:
    Array ( [229] => Array ( [hotel_id] => 229 [total] => 400 [number_of_rooms] => 0 [status] => r [PID] => 2088 [HID] => 229 [PLACENAME] => Rainbow Beach [hotelname] => Rainbow Ocean Palms Resort [destinationname] => Sunshine Coast [startype] => 5 ) ) */
    The code you wrote shows this:
    Code:
    Array ( [hotel_id] => 229 [total] => 400 [number_of_rooms] => 0 [status] => r [PID] => 2302 [HID] => 229 [PLACENAME] => Rainbow Beach [hotelname] => Rainbow Ocean Palms Resort [destinationname] => Sunshine Coast [startype] => 5 )
    Thank you
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    first of all, rephrase the "if" statement to avoid the cumbersome and duplicative code:

    Code:
    if lookup_entry isn't set OR current[total] < lookup_entry[total]
    	lookup_entry := current
    PHP Code:
    $lookup = array(); 
    foreach (
    $hotels_selection as $hotel)
        if ( !isset(
    $lookup[$hotel['hotel_id']]) || $hotel['total'] < $lookup[$hotel['hotel_id']] )
            
    $lookup[$hotel['hotel_id']] = $hotel
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  16. #9
  17. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,675
    Rep Power
    171
    Originally Posted by Jacques1
    Hi,

    first of all, rephrase the "if" statement to avoid the cumbersome and duplicative code:

    Code:
    if lookup_entry isn't set OR current[total] < lookup_entry[total]
    	lookup_entry := current
    PHP Code:
    $lookup = array(); 
    foreach (
    $hotels_selection as $hotel)
        if ( !isset(
    $lookup[$hotel['hotel_id']]) || $hotel['total'] < $lookup[$hotel['hotel_id']] )
            
    $lookup[$hotel['hotel_id']] = $hotel
    No! This is showing the biggest total (800) in stead of smallest. I even changed the < to > and still it shows the wrong one (700). It has to show total 400
    Why is this thing so difficult?
    Last edited by zxcvbnm; April 2nd, 2013 at 07:20 AM.
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by zxcvbnm
    Why is this thing so difficult?
    Because we all suck at programming.

    There's a ['total'] missing in my code:

    PHP Code:
    $hotel['total'] < $lookup[$hotel['hotel_id']]['total'
    We don't want to compare the total with the hotel ID.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  20. #11
  21. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,675
    Rep Power
    171
    Originally Posted by Jacques1
    Because we all suck at programming.
    It was pretty good successful effort, its working fine thanks.
    I still wonder what went wrong here in Requinx solution:
    PHP Code:
    $lookup = array();
    foreach (
    $hotels_selection as $key => $value) {
        if (isset(
    $lookup[$value["hotel_id"]])) {
            if (
    $hotels_selection[$lookup[$value["hotel_id"]]][0]["total"] < $value[0]["total"]) {
                
    $lookup[$value["hotel_id"]] = $key;
            }
        } else {
            
    $lookup[$value["hotel_id"]] = $key;
        }

  22. #12
  23. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by zxcvbnm
    I still wonder what went wrong here in Requinx solution
    It makes no sense to me. The "[0]" would only work if $hotels_selection was a numerical array of numerical arrays of associative arrays:

    Code:
    array(
    	0 => array(
    		array(
    			'total' => ...
    			...
    		)	
    	),
    	1 => array(
    		array(
    			'total' => ...
    			...
    		)	
    	),
    	...
    )
    But you don't have that structure (maybe you had when Requinix posted the code).

    Also the code fills $lookup with array indices ($key) rather than the actual hotels ($value).
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  24. #13
  25. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,675
    Rep Power
    171
    Originally Posted by Jacques1
    It makes no sense to me.
    Original post.
  26. #14
  27. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Well, obviously you did change the array structure. Before, all data like "total", "available_rooms" etc. was in a separate array with the index 0. Now it's all in one big associative array. So you changed the data structure without adjusting the code.

    Of course it would be much more efficient to do this selecting and sorting directly in the database. PHP isn't suited for processing large amounts of data. But I've seen that you already struggled with the query, so unless you experience massive performance issues, this workaround should be OK.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    397
    Rep Power
    189
    Originally Posted by zxcvbnm
    Why is this thing so difficult?
    Actually we've gotten you pretty close, you should be able to figure out the rest without us doing it all for you.

    Comments on this post

    • Jacques1 agrees
    • English Breakfast Tea agrees : Agreed
    Thomas Tremain
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo