Jump to content

Sorting A Multi dimensional array


holowugz

Recommended Posts

Hi guys here is what i am looking to do, the user dumps a large amount of text into a text box, and submits it here is an example of a line of the text:

2008-04-19 00:01:31: 67.71.153.73 Spud [?]

Basically date, time, IP, Username

 

I need these to be resorted to display by date and time.

 

//if user presses submit button
if($_POST['Submit']=="Submit"){
//Remove whitespace from beginning and end, and remove the [?] from the text
$_POST['textarea']= trim(str_ireplace(" [?] ","",$_POST['textarea']));
//insert into array, seperated by new line
$alpha=explode("\n",$_POST['textarea']);
$counter = 0;
//for every element in the array put it into array cheese (creating multi dimensional array)
foreach ($alpha as $value) {
$cheese["$counter"]=explode(" ",$value);
$counter++;
}
echo "</br>";

//sort array by date and then time
array_multisort($cheese[0],SORT_ASC,SORT_REGULAR,$cheese[1],SORT_ASC,SORT_REGULAR);

//debug function, remove before final release
var_dump($cheese);

echo "</br>";
echo "<table width=\"50%\" border=\"1\">
  <tr>
    <td>Date</td>
    <td>Time</td>
    <td>IP</td>
    <td>Name</td>
  </tr>";

foreach ($cheese as $value) {
echo "  <tr>
    <td>$value[0]</td>
    <td>$value[1]</td>
    <td>$value[2]</td>
    <td>$value[3]</td>
  </tr>";

}
echo "</table>";
}

 

The problem is it will NOT sort correctly i just get a jumbled mess, can anyone help?

Link to comment
Share on other sites

The way I have handled this in the past is to convert them to a simple array with an "/" as a separator

$x=0;

# start loop1

$new_array1[]="$time..../$ip.../$data.../$etc....";

$x++;

# end loop1

sort($new_array1);

$x=0;

# start loop2

$new_array2=explode("/",$new_array1[$x]);

# use the new array or put it back sorted

$x++;

# end loop2

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.