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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.