...

View Full Version : Array sorting by third key



blinn_shade
01-27-2012, 10:28 PM
Hey,

I ahve this array I was wondering if someone could help me sort:


Array
(
[administration] => Array
(
[admin_main] => Array
(
[2] => Array
(
[section_path] => content/
)

)

[admin_reports] => Array
(
[1] => Array
(
[section_path] => content/
)

)

)
}

I need to be able to sort it by the third key. As you see 2,1. It need to be 1,2. This is not the full array as it would take up a long page. The administration key is not always administration.

Thanks in advance.

Fou-Lu
01-28-2012, 03:26 AM
If I understand what you are looking for, you are trying to sort then entire array's administration association based on the first level key value of the admin_main or admin_reports correct?
Use a uasort:


function cmpAdministrationASC($a, $b)
{
$fv = key($a);
$sv = key($b);
return $fv - $sv;
}

uasort($array['administration'], 'cmpAdministrationASC');

blinn_shade
01-28-2012, 04:18 PM
Hey,

Thanks for the reply.

Here is the full array:


Array
(
[administration] => Array
(
[admin_main] => Array
(
[2] => Array
(
[section_path] => content/
)

[1] => Array
(
[section_path] => content/
)

)

[admin_reports] => Array
(
[3] => Array
(
[section_path] => content/
)

[1] => Array
(
[section_path] => content/
)

)

)

[another] => Array
(
[soemthing_else] => Array
(
[2] => Array
(
[section_path] => content/
)

[7] => Array
(
[section_path] => content/
)

)

[something] => Array
(
[5] => Array
(
[section_path] => content/
)

[2] => Array
(
[section_path] => content/
)

)

)
}

basically I need to order the 3rd level by the numbers. Any ideas mate?

Fou-Lu
01-28-2012, 04:26 PM
But keep the rest of the items in place?
Its doable, but I don't think I'd recommend the approaches since they are all either iterative, recursive, or direct pull of each element out of the array. How is this array created in the first place; that is likely where it would be best to sort the last level instead of waiting until after the array is fully constructed.

blinn_shade
01-28-2012, 05:04 PM
Hey,

This is a menu system. It is pointless me pasting the code as it references classes which you would need to look through about 3 class files.

Would you be able to help me sort this mate?

Fou-Lu
01-28-2012, 05:16 PM
If these are object that have been decayed into an array you can write a comparator for the objects themselves and then invoke a usort prior to the decay cast. If they are not objects at the point they have been added to an array, since these all have associative and unknown associative offsets, then you'll need to write an entire sort for it, which will be complex since it needs to get to the third level of unknown offsets.
The easiest solution is to go back to where the last level is added and use a ksort on it at that point.

blinn_shade
01-28-2012, 05:39 PM
Ah ok.

Here is the part which loops the folders and creates the array:


$full_path = $section_array['path'];

if ($handle = opendir($full_path)) {
while (false !== ($folder = readdir($handle))) {
if(is_dir($full_path.$folder) && $folder != '.' && $folder != '..' && $folder != 'cmd'){



$this->content_group_array[$section_array['folder']][$folder][$this->config_file_array[$folder]['sort']] = array(

'folder' => $folder,

'name' => $this->config_file_array[$folder]['name'],
'icon' => $full_path.$folder.'/icon.png',

'link' => $section_array['folder'].'/'.$folder.'/',
'path' => $full_path.$folder.'/'

);
}
}
}

Where would I put it for this, would you know?

The code above is inside a loop so I guess it would be somewhere outside this code?

Thanks

Fou-Lu
01-28-2012, 06:00 PM
Okay, I see what you are doing here.
Here's what you need to do. We need to bring an array up to the level of the 'folder'. We don't care what this key is since it will be iterated and sorted after. It *can* be sorted within the loop, but I recommend against this for two reasons: its more complicated, and using a sort within a location that modifies the data itself can often lead to issues such as infinite loops.


if ($handle = opendir($full_path)) {
$aFolders = array();
while (false !== ($folder = readdir($handle))) {
if(is_dir($full_path.$folder) && $folder != '.' && $folder != '..' && $folder != 'cmd'){



$aFolders[$this->config_file_array[$folder]['sort']] = array(

'folder' => $folder,

'name' => $this->config_file_array[$folder]['name'],
'icon' => $full_path.$folder.'/icon.png',

'link' => $section_array['folder'].'/'.$folder.'/',
'path' => $full_path.$folder.'/'

);
}
}
ksort($aFolders);
$this->content_group_array[$section_array['folder']] = array_merge($this->content_group_array[$section_array['folder']], $aFolders);
}

So if I understand the structure you have here, this appears that it will work. I assumed that $section_array is also a grouping level of some sorts which would indicate a loop level above this somewhere.

blinn_shade
01-28-2012, 06:27 PM
Hey,

Sorry fi this takes your time up mate. I really can't thank you enough for helping me. I did put in the code but it through an error at me.

Here is the full function below.


function structure_sections_content_group_array(){

foreach($this->section_array as $section_sort){

foreach($section_sort as $section_section => $section_array){

$full_path = $section_array['path'];

if ($handle = opendir($full_path)) {
while (false !== ($folder = readdir($handle))) {
if(is_dir($full_path.$folder) && $folder != '.' && $folder != '..' && $folder != 'cmd'){

$this->structure_read_config_file('content_group.config', $full_path.$folder, $folder);

$this->content_group_array[$section_array['folder']][$folder][$this->config_file_array[$folder]['sort']] = array(

'folder' => $folder,

'name' => $this->config_file_array[$folder]['name'],
'icon' => $full_path.$folder.'/icon.png',

'link' => $section_array['folder'].'/'.$folder.'/',
'path' => $full_path.$folder.'/'

);
}
}
}

}

//usort($this->content_group_array);
}

//echo'CONTENT GROUP ARRAY <pre>'; print_r($this->content_group_array); echo'</pre>';
$this->config_file_array = array();
}

$this->section_array has an array like this:


Array
(
[administration] => Array
(
[100] => Array
(
[folder] => administration
[name] => Admin
[link] => administration/
[path] => content/administration/
)

)

[catalogue] => Array
(
[2] => Array
(
[folder] => catalogue
[name] => Catalogue
[link] => catalogue/
[path] => content/catalogue/
)

)

)

Thanks for your help.

Fou-Lu
01-28-2012, 07:54 PM
These outputs are all becoming different. Section array doesn't follow the same format as your previous arrays.
What is the error you had?

blinn_shade
01-28-2012, 08:26 PM
Hey,

This is a menu structure.

The section_array is the top level.
Then you have content_group_array which is the second level
and finally you have content_array which is the third and last level ( I haven't mentioned this before)

This is the error I get:

Error: [8] Undefined index: administration
Error in C:\Zend\Apache2\htdocs\admin\inc\class\system\structure.php on line 121 : Undefined index: administration

blinn_shade
01-28-2012, 11:39 PM
or instead of the the last key being the number I can put it in the array like this if it would work better?


Array
(
[administration] => Array
(
[admin_main] => Array
(

[section_path] => content/
[sort] => 2


)

[admin_reports] => Array
(

[section_path] => content/
[sort] => 1


)

)
}

Fou-Lu
01-29-2012, 04:51 PM
No, modifying the structure doesn't look like it will work. You have multiple section_path below admin_main, so that has to be an array.
Also, I appear to have biffed this as well. I don't think this should array_merge since the array is full and complete after the loop, and doesn't look like section_array[folder] section exists before this point.
Change this:


$this->content_group_array[$section_array['folder']] = array_merge($this->content_group_array[$section_array['folder']], $aFolders);

to this:


$this->content_group_array[$section_array['folder']] = $aFolders;

blinn_shade
01-30-2012, 03:37 PM
THanks mate. I decided not to order them now as the folder names order it anyway. Sorry if this troubled you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum