tomasz333 Posted February 23, 2008 Share Posted February 23, 2008 I am trying to arrange data alphabetically based on what is found inside the exploded array. The code I have used to get all variables is as follows: ... ob_start(); include ($inc."/somefile.txt"); $allinformation = ob_get_contents(); ob_end_clean(); $eachrow = explode("\n",$allinformation); for($i = 0; $i < count($eachrow); $i++){ $eachpart = explode("\t",$eachrow[$i]); echo ... Does anyone know what I would have to do if I wanted to arrange the rows ($eachrow) in a way that would allow each section ($eachpart[1]) to be arranged alphabetically? A visual example of the lines parsed would be as follows: something else[tab]Bob[tab]55[tab]something else[tab]and so forth something else[tab]Anne[tab]170[tab]something else[tab]and so forth something else[tab]Someone Else[tab]5000[tab]something else[tab]and so forth Where the desired result would be an alphabetical arrangement of the rows based on the names given: something else[tab]Anne[tab]170[tab]something else[tab]and so forth something else[tab]Bob[tab]55[tab]something else[tab]and so forth something else[tab]Someone Else[tab]5000[tab]something else[tab]and so forth Thanks are offered in advance. Quote Link to comment https://forums.phpfreaks.com/topic/92594-arranging-explodes/ Share on other sites More sharing options...
Chris92 Posted February 23, 2008 Share Posted February 23, 2008 yes, you could use the sort() function: $eachrow = explode("\n",$allinformation); sort($eachrow); for($i = 0; $i < count($eachrow); $i++) { $eachpart = explode("\t",$eachrow[$i]); } Quote Link to comment https://forums.phpfreaks.com/topic/92594-arranging-explodes/#findComment-474526 Share on other sites More sharing options...
tomasz333 Posted February 23, 2008 Author Share Posted February 23, 2008 I've tried a various combinations of sort but I can't get it to work because it uses the whole string (as opposed to a subsection as marked by $eachpart -- which is what I want it to use when determining alphabetical order). I wish to use it on the second set of explosions but still apply it to order of the first: c b a originally first b a c originally second a c a originally third would become b a c originally second c b a originally first a c a originally third Quote Link to comment https://forums.phpfreaks.com/topic/92594-arranging-explodes/#findComment-474537 Share on other sites More sharing options...
Bauer418 Posted February 23, 2008 Share Posted February 23, 2008 You'll can use usort (sorting based on a custom user-callback function) to sort based on a child of an array. Quote Link to comment https://forums.phpfreaks.com/topic/92594-arranging-explodes/#findComment-474544 Share on other sites More sharing options...
wildteen88 Posted February 23, 2008 Share Posted February 23, 2008 Could use something like this: <?php // get data from file $lines = file('data.txt', FILE_IGNORE_NEW_LINES); // loop through data $tempDataArray = array(); foreach($lines as $line) { // get individual bits of data from tab delimited list list($col1, $name, $number, $col4, $col5) = explode("\t", $line); // Add data to a temprory array, assign the name field as the key for the new child-array $tempDataArray[$name] = array($col1, $number, $col4, $col5); } // sort temprory array. Ksort sorts array keys alphabetically. This is why we used the name field as the key ksort($tempDataArray); // for all data stored in our temprory array, we'll need to convert it back into a tab delimited list // initiate $newData variable as an empty string $newData = ''; // loop through temprory data array foreach($tempDataArray as $key => $bitsArray) { // pull data from sub array list($col1, $number, $col4, $col5) = $bitsArray; // concat each line into the newData string $newData .= "$col1\t$key\t$number\t$col4\t$col5\n"; } // write data to file file_put_contents('sortedData.txt', $newLine); ?> Quote Link to comment https://forums.phpfreaks.com/topic/92594-arranging-explodes/#findComment-474625 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.