Jump to content

array sort help


ShortSchoolBus

Recommended Posts

Hi,

 

I'm having some difficulty with this and was hoping someone can show me an efficient way to sort an array such as this

 

array('1', '2-1', '2-2-1', '2-2', '2', '3-1', '3');

 

to

 

array('1', '2', '2-1', '2-2', '2-2-1', '3', '3-1');

 

with no length restrictions. eg., 3-3-2-2-1 or 4-5-4-3-3-2-1

 

Thanks in advance for your help.

Link to comment
https://forums.phpfreaks.com/topic/152152-array-sort-help/
Share on other sites

No, actually, regular sort will return the same array('1', '2-1', '2-2-1', '2-2', '2', '3-1', '3');

 

This is how a file system is sorted by default, but I need the result to be array('1', '2', '2-1', '2-2', '2-2-1', '3', '3-1');.

 

I hope you understand what I mean.

Link to comment
https://forums.phpfreaks.com/topic/152152-array-sort-help/#findComment-799082
Share on other sites

I came up with a solution, but it's not very efficient

 

$pages = array('page_1.php', 'page_2-1.php', 'page_2-2-1.php', 'page_2-2.php', 'page_2.php');
$pos_array = array();
foreach ($pages as $page) {
$page_name = substr($page, 0, strpos($page, ".php"));
    $parts = explode("_",$page_name);
    $position_string = $parts[1];
array_push($pos_array,$position_string);
}
sort($pos_array);
$page_array = array();
foreach ($pos_array as $pos) {
foreach ($pages as $page) {
	$page_name = substr($page, 0, strpos($page, ".php"));
        $parts = explode("_",$page_name);
        $position_string = $parts[1];
	if($pos == $position_string){
	array_push($page_array,$page);	
	}
	}
}
$pages = $page_array;

 

Output:

[0] => page_1.php

[1] => page_2.php

[2] => page_2-1.php

[3] => page_2-2.php

[4] => page_2-2-1.php

 

Link to comment
https://forums.phpfreaks.com/topic/152152-array-sort-help/#findComment-799124
Share on other sites

dunno if its necessarily faster or not, but you could also do this:

 

$array = array('page_1.php', 'page_2-1.php', 'page_2-2-1.php', 'page_2-2.php', 'page_2.php', 'page_3-1.php', 'page_3.php'); 

function format(&$file, $key, $t) {
   $file = ($t == 'strip')? preg_replace('~(page_|\.php)~','',$file) : 'page_' . $file . '.php';
}
array_walk($array, 'format','strip');
sort($array);
array_walk($array, 'format','build');

Link to comment
https://forums.phpfreaks.com/topic/152152-array-sort-help/#findComment-799147
Share on other sites

and to tweak it a bit more...

function DoSort($a, $b)
{	
$strip = array('page_', '.php');
return (str_replace($strip, '', $a) < str_replace($strip, '', $b)) ? -1 : 1;
}

$array = array('page_1.php', 'page_2-1.php', 'page_2-2-1.php', 'page_2-2.php', 'page_2.php', 'page_3-1.php', 'page_3.php');
usort($array, 'DoSort');

Link to comment
https://forums.phpfreaks.com/topic/152152-array-sort-help/#findComment-799176
Share on other sites

My intention is to use the filename to determine it's position/depth within a list.

Example -

<ul>
<li>page_1.php</li>
<li>page_2.php
<ul>
<li>page_2-1.php</li>
<li>page_2-2.php</li>
</ul>
</li>
<li>page_3.php
<ul>
<li>page_3-1.php</li>
</ul>
</li>
</ul>

 

If anyone has a suggestion for an efficient method for this, please post.

 

Link to comment
https://forums.phpfreaks.com/topic/152152-array-sort-help/#findComment-799404
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.