Jump to content

Sorting multidimensional array


datafan

Recommended Posts

Everything works great, now I just want to sort the array so my output is in the correct order. This is just the portion of the code where I am trying to sort $line.

$line looks like this:

 

$line[0]    $line[1]    $line[2]    $line[3]      $line[4]

1111111    c1            0800        1600          www.blahblahblan.blah

 

$line[0] is an epoch time string.

$line[1] is a classroom name and will be one of the following: c1,c2,c3,c4,c5,c6,MN.

I want to sort first by classroom name $line[1], then by epoch time $line[0], while maintaining each "line" of data.

So my calendar entries are grouped by classroom and then the time the room is reserved.

I just need to figure out how to sort the array. Thanks...

 

Here is my sort code:

 

<?php
foreach($flatlist as $row) {
	$line = explode("|", $row);
	$month = date ("n", $line[0]);
	if ($month == $ChosenMonth) {
	array_multisort($line[1], SORT_ASC, $line[0], SORT_ASC, $line); //<---line 51, right here is what I can't seem to get right

// lot's more code.....
?>

 

My last error:

 

Warning: array_multisort(): Argument #1 is expected to be an array or a sort flag in /home/tamstouc/public_html/calcreate.php on line 51

 

 

 

 

 

Link to comment
Share on other sites

Ok first the reason that array_multisort isn't working for you, is that $line[1] in the place that it's at in your code is a value not an array.  What you should do is finish out the foreach loop and then sort it, Something like:

 

     for ($i=count($flatlist) -1; $i >= 0; $i--){
           $line = explode("|", $flatlist[$i]);
           $time = $line[0];
           $classroom = $line[1];
           $result[$classroom][$time][] = array(
                  'line2' => $line[2],
                  'line3' => $line[3],
                  'line4' => $line[4]);
           }            

 

I wrote that without any shortcuts to help you see what's happing.  Also I use smarty for almost everything so this creates a variable that is more appropriate for what I would use, and easier to manage.  This would generate a variable like:

 

array(
  [classroom1] => array(
     [date] => array(
        [0] => array(
           [line2] => 'Some Value',
           [line3] => 'Some Value',
           [line4] => 'Some Value')
           )
        )
   )
)

 

Now you can do a KSort on the classrooms with:

ksort($result)

 

That will sort out by classroom...  Now when you loop through the array to display classrooms you can also ksort by time

 

//Loop through all classrooms
foreach ($result as $classroom => $value){
     ksort($classroom); //Sorts by date;
     //Loop Through By Date
     foreach ($classroom as $date => $info){
         print "Classroom = $classroom <br />";
         print "Date = ".date ("n", $date)." <br />";
         print "Line2 = ".$info['line2']." <br />";
         print "Line3 = ".$info['line3']." <br />";
         print "Line4 = ".$info['line4']." <br />";
         }
     }

 

Hope this helps

   

Link to comment
Share on other sites

Barand,

 

I don't understand your answer.  First in order to sort the requested information he's first got to get all the information into something usable.  You're solution would only sort the line, and not sort out the results of all classrooms and dates.

 

The way I see it (and please correct me if I'm wrong), if he preformed your usort it wouldn't work as expected.  First he'd have to sort by date, which would work, but then when he sent it back through your usort to sort by school the array would get jumbled up.

 

If he completed the array, the best approach would be array_multisort, or in my example ksort, to sort it out. 

 

Can you give me a code snippet to show how he would use usort to get the results he's looking for, I am curious.

 

Sorry if this post is a little direct or offensive, it's really ment to be a honest question to find out more about how this would work and not an accusation.

 

 

 

Link to comment
Share on other sites

Here you are

 

<?php
$data = array (
               array (3, 'c1', '0800', '1600', 'www.blahblahblan.blah'),
               array (4, 'c3', '0800', '1600', 'www.blahblahblan.blah'),
               array (2, 'c1', '0800', '1600', 'www.blahblahblan.blah'),
               array (4, 'c2', '0800', '1600', 'www.blahblahblan.blah'),
               array (1, 'c3', '0800', '1600', 'www.blahblahblan.blah'),
               array (8, 'MN', '0800', '1600', 'www.blahblahblan.blah'),
               array (1, 'c1', '0800', '1600', 'www.blahblahblan.blah'),
               array (2, 'c5', '0800', '1600', 'www.blahblahblan.blah'),
               array (3, 'c4', '0800', '1600', 'www.blahblahblan.blah'),
               array (6, 'c4', '0800', '1600', 'www.blahblahblan.blah'),
               array (2, 'c3', '0800', '1600', 'www.blahblahblan.blah'),
               array (7, 'MN', '0800', '1600', 'www.blahblahblan.blah'),
);

function mysort($a, $b)
{
    $sc = strcmp($a[1], $b[1]);     // compare classrooms
    if ($sc==0)                     // if they're equal, compare epoch
    {
        return $a[0] - $b[0];       // if a should sort above b, return -ve number
    }
    else return $sc;
}

usort ($data, 'mysort');            // sort using custom function

echo '<pre>', print_r($data, true), '</pre>';      // show sorted results
?>

Link to comment
Share on other sites

First of all, thanks to both of you for the help. I must admit though, due to my inexperience for sure, I can't get either of the methods to work.

 

lachild, I kept getting an error on the

 <?php ksort($classroom); //Sorts by date; ?> 

Saying something to the effect that it was looking for an array and was getting a string.

 

Barand, I can't seem to get my data from the flat file into an array, I can only loop through it one "line" at at time so there is nothing to sort. How can I simply build an array that I can sort outside my "for" loop?

 

Thanks again for your help.

 

 

Link to comment
Share on other sites

oops...  You're right... It should have been

ksort($value)

 

sorry about that..

 

As for Barand's method... Forget about doing everything in one loop. You have to get the data into a usable format before you can do anything to the data.  As you get better you'll start to figure out shortcuts and ways to streamline your code, but while you're learning, take it slow.  Remember the computer can really only do one task at a time anyway.

 

So basically to work with the flat file you need to put it into an array.  Like this...

 

foreach($flatlist as $row) {
     $line = explode("|", $row);
     $data[] = array ($line[0], $line[1], $line[2], $line[3], $line[4]);
     }

 

Now the data will be in a usable format, Now you can do anything you want to data including:

usort ($data, 'mysort');

 

 

PS. The $data[] in the loop tells the computer to store the new data into a new array.  So if $data[0] is already set then the next row will be assigned $data[1] and so on, until it reaches the end of the loop.

 

Hope that helps

Link to comment
Share on other sites

Thanks a bunch, I got the result I was looking for using a combination of both our methods.

 

<?php
$flatlist = file("masterflat.txt");
foreach ($flatlist as $row) {
for($i=count($row) -1; $i >= 0; $i--){
           $result = explode("|", $row);
           $line[] = array($result[0], $result[1], $result[2], $result[3], $result[4]);
           }
}
/*echo "<pre>";		   
print_r($result);
echo "</pre>";*/

function mysort($a, $b)
{
    $sc = strcmp($a[1], $b[1]);     // compare classrooms
    if ($sc==0)                     // if they're equal, compare epoch
    {
        return $a[2] - $b[2];       // if a should sort above b, return -ve number
    }
    else return $sc;
}

usort ($line, 'mysort');            // sort using custom function

echo '<pre>', print_r($line, true), '</pre>';      // show sorted results
?>

 

outputs this, exactly what I was looking for!

 

Array

(

    [0] => Array

        (

            [0] => 1199174400

            [1] => c1

            [2] => 0600

            [3] => 0830

            [4] => 01-01~01-02c1600-830.txt

 

        )

 

    [1] => Array

        (

            [0] => 1199260800

            [1] => c1

            [2] => 0600

            [3] => 0830

            [4] => 01-01~01-02c1600-830.txt

        )

 

    [2] => Array

        (

            [0] => 1199174400

            [1] => c1

            [2] => 0830

            [3] => 1630

            [4] => 01-01~01-03c1c2830-430.txt

 

        )

 

    [3] => Array

        (

            [0] => 1199347200

            [1] => c1

            [2] => 0830

            [3] => 1630

            [4] => 01-01~01-03c1c2830-430.txt

 

        )

 

    [4] => Array

        (

            [0] => 1199260800

            [1] => c1

            [2] => 0830

            [3] => 1630

            [4] => 01-01~01-03c1c2830-430.txt

 

        )

 

    [5] => Array

        (

            [0] => 1199174400

            [1] => c2

            [2] => 0830

            [3] => 1630

            [4] => 01-01~01-03c1c2830-430.txt

 

        )

 

    [6] => Array

        (

            [0] => 1199260800

            [1] => c2

            [2] => 0830

            [3] => 1630

            [4] => 01-01~01-03c1c2830-430.txt

 

        )

 

    [7] => Array

        (

            [0] => 1199347200

            [1] => c2

            [2] => 0830

            [3] => 1630

            [4] => 01-01~01-03c1c2830-430.txt

 

        )

 

)

 

Thanks again!

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.