Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-08-2013, 04:10 PM   PM User | #1
zugzwangle
New to the CF scene

 
Join Date: Mar 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
zugzwangle is an unknown quantity at this point
Sorting subarrays by another array keys

Hi there all,

Thank you for your suggestions and help in anticipation of your help!!

I have 2x arrays. The first is an array of the totals for each round. The second is an array split into weeks (the weeks being the first level keys. I would like to arrange all of the second subarray team names, in order of the teamnames of the first array. so in order of [team3], [team5], [team1], [team2] and [team4].. I hope you follow what I mean.. Here are the first and second arrays!!


First array (Total of rounds)

Code:
Array
(
    [Team3] => Array
        (
            [brd_points] => 14
            [ttl_points] => 5
            [games] => 3
        )

    [Team5] => Array
        (
            [brd_points] => 12
            [ttl_points] => 3
            [games] => 2
        )

    [Team1] => Array
        (
            [brd_points] => 7
            [ttl_points] => 3
            [games] => 2
        )

    [Team2] => Array
        (
            [brd_points] => 16
            [ttl_points] => 1
            [games] => 3
        )

    [Team4] => Array
        (
            [brd_points] => 7
            [ttl_points] => 0
            [games] => 2
        )

)1
// Second array - individual rounds.

Array
(
    [1] => Array
        (
            [Team3] => Array
                (
                    [brd_points] => 4
                    [ttl_points] => 2
                )

            [Team2] => Array
                (
                    [brd_points] => 6
                    [ttl_points] => 0
                )

            [Team4] => Array
                (
                    [brd_points] => 6
                    [ttl_points] => 0
                )

            [Team1] => Array
                (
                    [brd_points] => 2
                    [ttl_points] => 2
                )

            [Team5] => Array
                (
                    [brd_points] => Bye
                    [ttl_points] => Bye
                )

        )

    [2] => Array
        (
            [Team3] => Array
                (
                    [brd_points] => 5
                    [ttl_points] => 2
                )

            [Team4] => Array
                (
                    [brd_points] => 1
                    [ttl_points] => 0
                )

            [Team5] => Array
                (
                    [brd_points] => 7
                    [ttl_points] => 2
                )

            [Team2] => Array
                (
                    [brd_points] => 5
                    [ttl_points] => 0
                )

            [Team1] => Array
                (
                    [brd_points] => Bye
                    [ttl_points] => Bye
                )

        )

    [3] => Array
        (
            [Team2] => Array
                (
                    [brd_points] => 5
                    [ttl_points] => 1
                )

            [Team3] => Array
                (
                    [brd_points] => 5
                    [ttl_points] => 1
                )

            [Team5] => Array
                (
                    [brd_points] => 5
                    [ttl_points] => 1
                )

            [Team1] => Array
                (
                    [brd_points] => 5
                    [ttl_points] => 1
                )

            [Team4] => Array
                (
                    [brd_points] => Bye
                    [ttl_points] => Bye
                )

        )

)
1
Again thanks for your help!!
zugzwangle is offline   Reply With Quote
Old 02-08-2013, 05:28 PM   PM User | #2
zugzwangle
New to the CF scene

 
Join Date: Mar 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
zugzwangle is an unknown quantity at this point
Found an answer

I found an answer!! Thanks for reading.
PHP Code:
function sort_array($array$orderArray) {
            
$ordered = array();
            foreach(
$orderArray as $key => $value) {
                if(
array_key_exists($key,$array)) {
                        
$ordered[$key] = $array[$key];
                        unset(
$array[$key]);
                }
            }
            return 
$ordered $array;
        } 
zugzwangle is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:54 AM.


Advertisement
Log in to turn off these ads.