Jump to content

Arranging Explodes


tomasz333

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/92594-arranging-explodes/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/92594-arranging-explodes/#findComment-474537
Share on other sites

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);

?>

Link to comment
https://forums.phpfreaks.com/topic/92594-arranging-explodes/#findComment-474625
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.