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
https://forums.phpfreaks.com/topic/107388-sorting-a-multi-dimensional-array/
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

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.