• Notice regarding image hosts and attachments. See here.

PHP Array Sort problem...

Associate
Joined
30 Dec 2005
Posts
414
Greeting to all!

I've hit a problem whilst sorting multiple PHP arrays... the odd thing is it organised some of the elements correctly, but not the others...

Ok so i've got 3 arrays (Sections '$files', Pages '$files2' and Order '$order').

Code:
$files array
(
    [0] => modules
    [1] => templates
    [2] => tmpuploader
    [3] => components
    [4] => contact_us
    [5] => administrator
    [6] => newstmp
    [7] => cubs
)

$files2 array
(
    [2] => Array
        (
            [0] => status.php
            [1] => upload.php
            [2] => upload_progress.php
        )

    [4] => Array
        (
            [0] => callback.php
        )

    [6] => Array
        (
            [0] => admin.php
            [1] => archive.php
            [2] => auth.php
            [3] => headlines.php
            [4] => help.php
            [5] => news.php
            [6] => sendtofriend.php
            [7] => settings.php
        )

    [7] => Array
        (
            [0] => badges_and_awards.php
            [1] => test.php
        )

)

$order array
(
    [0] => 2
    [1] => 3
    [2] => 0
    [3] => 1
    [4] => 8
    [5] => 4
    [6] => 5
    [7] => 6
)

Now what I wanted it to do is reorganise the $files array based on the values present in $order. However, to do that successfully, it also needs to reorganise the values found in $files2.

Fortunately, i've got it doing this, and it works, but when it does the cubs section, it gets rid of all the values for 'cubs' found in $files2!

Code:
$files array
(
    [0] => tmpuploader
    [1] => components
    [2] => modules
    [3] => templates
    [4] => administrator
    [5] => newstmp
    [6] => cubs
    [7] => contact_us
)

$files2 array
(
    [2] => 
    [3] => 
    [0] => Array
        (
            [0] => status.php
            [1] => upload.php
            [2] => upload_progress.php
        )

    [1] => 
    [7] => Array
        (
            [0] => callback.php
        )

)

$order array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 8
)

Here is the code if anyone has any ideas to the problem:
Code:
print_r($files);
print_r($files2);
print_r($order);
array_multisort($order,$files); //Order the sections based on the order IDs

//Because array_multisort can only handle 2 arrays, do the $files2 array by hand
$files3 = array();
for($i=0;$i<=count($files2);$i++) {
	$value = $tmpfiles[$i];
	$key = array_search($value,$files);
	$files3[$key] = $files2[$i];
}
print_r($files);
print_r($files3);
print_r($order);

As always, any thoughts, help etc is appreciated :D
 
marc2003 said:
1) regarding the cubs bit, you have no "7" in the values of your $order array.
That is actually correct... hard to explain but it's basically a hidden section which shouldn't be included in the array. I wouldn't have thought that missing an item in the array would cause the problem of getting rid of other elements though..

marc2003 said:
2) why can't you create the arrays in the right order to begin with? all this info looks static. sorting should be done on dynamically created arrays surely? (i don't actually know that but that's the way i see things.... :p)
The arrays are actually dynamic.. they're taken fresh from the filesystem and a database. They are then sorted into the correct order, and an XML structure file is produced, which the website's menus etc work off :)
 
Back
Top Bottom