Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort array by another array or key in X order 2

Status
Not open for further replies.

craigey

Technical User
Apr 18, 2002
510
GB
Hi All,

I have an array that contains a list of 10 files. I want to change the order of some of the elements in the array so whe I do a for each loop the first three elements are always in the same order.

eg:

Code:
$contentfiles = glob("" . $files . "*.php");

$correctorder = array ('0' => "./index.php", '1' => "./services.php", '2' => "./map.php", '3' => "", '4' => "", '5' => "", '6' => "", '7' => "", '8' => "", '9' => "", '10' => "");

Alternatively is there someway to manipulate the key?

Code:
$contentfiles = glob("" . $files . "*.php");

//not actual code but what I'm trying to explain :-)

$correctorder = array ('2','4','5','0','1','3');

$contentfiles[$key]=$correctorder[key];

foreach ($contentfiles as $filename){

blah
}
 
Code:
<?php
$contentfiles = glob("" . $files . "*.php");
$correctorder = array ("./index.php","./services.php","./map.php"); //just profile the files you want frontloaded
foreach ($correctorder as $i):
  $keys = array_keys($contentFiles, $i); //locate key of item
  unset($contentfiles[$keys[0]]); //remove the array items from $contentFiles
endforeach;
$files = array_merge($correctorder, $contentfiles);
?>
 
Thanks. I think I might save that one as a useful function.
 
jpadie Thank You I am keeping this as a ref.. I've have tryed to do this before and failed!!! Now I see My error!!!

MA WarGod

I believe if someone can think it, it can be programmed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top