Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Associative Arrays

Options
  • 18-02-2005 3:59pm
    #1
    Registered Users Posts: 166,026 ✭✭✭✭


    Hi,

    I have an associative multidimensional array.

    $retval["country"][$x]
    $retval["county"][$x]
    $retval["contact"][$x]
    $retval["address"][$x]
    etc

    type array.

    This array is poulated with (for example), the details of companies. The end-user can search for companies in a certain individual county (Dublin say) OR they can tick a box and search for the individual county (Dublin) and surrouding counties (Wicklow, Kildare also retrieved).

    My problem is, with the display, I want the results from Dublin to appear first and then the surrounding counties after it.

    Becuase of the associative array, I cannot seem to be able to use array_shift.
    I'd like to shuffle the array so that any ones with

    $retval["county"][$x] = $_GET["county_id"] get put to the top of the array.

    Anyone got any hints or tips. First person to reply with an answer gets a drink at the bar.

    Any help or advice much apreciated.

    Thanks,

    B


Comments

  • Registered Users Posts: 277 ✭✭Lawnkiller


    Its been a while since I've done PHP but my solutions could work regardless of programming language.

    1) Presumably, you are extracting that information from a database. You could try performing your sort in the SQL, etc. then...

    2) If you just have the info in tha associative array, you could create another array and copy the info into your new array in the order you need. Then ditch the old array and render your new sorted array as you need...

    .. :) I leave the floor open to anyone who wants the drink. Let me know if this help anyway...


  • Registered Users Posts: 568 ✭✭✭phil


    It makes more logical sense, to me anyways, to have the arrays associated by index first.

    e.g.

    $retval[$x]["country"]

    I'm presuming $x is normally represented by an integer value which is basically an index into the array to group individual records together. It would make life easier if the index was the first grouping and then broken down after. However, this isn't necessarily relevant.

    Below the code makes some assumptions, it also causes the indexes to be moved which means that you need to move all the indexes for all the elements in your first associative array if you need to use this data again.

    If you want to keep the indexes in place and simply print it differently, the code could be changed. It's much easier to analyze a piece of code with data, so if this doesn't suit, perhaps you can post a small example up?
    <?
    $sel = "Galway";
    
    $a = array (
       "county" => array ("Dublin", "Cork", "Galway"),
       "name" => array ("John", "James", "Jack")
       );
    
    
    
    $len_a = count($a["county"]);
    
    for ($i = 0; $i < $len_a; $i++)
    {
       if ($a["county"][$i] == $sel)
       {
          $temp_val = $a["county"][$i];
          unset($a["county"][$i]);
          $a["county"] = array_merge(array($temp_val), $a["county"]);
       }
    }
    print_r($a);
    
    ?>
    
    


Advertisement