1

Instead of working with mysql data, I have created csv file that I plan to use as the source of data for content, etc.

And I have successfully been able to parse the csv and store it into a complex array, that has the following header row aka the keys for the arrays.

"Title","Year","Rated","Released","Runtime","Genre","Director","Writer","Actors","Plot","Language","Country","Awards","Poster","Metascore","imdbRating","imdbVotes","imdbID","Type","Response"

My current stage is to allow dynamic ajax sorting of the arrays.

I have two fields, that I am allowing sorting at the beginning, "Year" and "Title".

So I pass different url paramters, such as "yearasc" or "yeardesc" or "titleasc" or "titledesc".

Then try to sort for that.

So what I did reading a different so post, was to do this.

First I create new arrays that only store the key fields, I need for sorting.

Then based on what sort type, do a different array_multisort.

array_multisort($year, SORT_ASC, $all_rows);

But what I get is results that multiple dupplicate data.

But I wonder if having the first row, be the header row, which is required by the function I pass the data after any sorting to, is causing issues with array sorting.

For simple array sorting, existing functions makes sense and work fine.

But for complicated ones, it is just complex to even understand how to approach solving this problem.

Any suggestions, thoughts or ideas are appreciated and thanked.

Thank you!

1
  • 1
    Question: Is there any particular reason why you chose a csv file over a database option? Commented Jul 21, 2014 at 6:13

2 Answers 2

3

I don't have the actual code that is probably going to help you, but I do have a suggestion as for how you can tackle this and make it work..

Keep it simple. First, create your own CSV with just 1 header (Year or Title ) that you want to sort on.

Write your code to sort on that.

Then, add the other one ( Title if you used Year before, or Year if you used Title before ) and sort on whichever you want.

Then, add one more header (say, Rated) that you don't want to sort on.

You should then be able to work with the original CSV.

I'd try to write simple methods and keep your processing to a minimum in each one.

I hope that helps. I realize its more philosophical of an answer, so it is hit or miss if it helps you get the job done. Just realize that this approach will, indeed, take a little more time to write - but the point behind it is that you're taking out all of the "noise" that's getting in your way first. It helps you look at only your problem first and solve that.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm up voting this because you have a fair few valid points here and it almost impossible to answer the question until the OP supplies his attempts at this.
I don't want to re-work the creation of the csv, I just want to take the csv, I have already created, and stored in an array, and now allow me to be able to sort by it. Was that clear?
Yes, that's clear. I realize that you have the array already. The thing I'm suggesting is that instead of trying to solve the problem with a HUGE array - try to solve it with a small array instead. Work your way up to handling the large array as the end result. The steps outlined above are just a suggestion in how to get to that point
1

You can set a custom sort function to the array. Use asort() if you need to keep original array keys.

<?php
$sortfields = array('year', 'bleh');

function cmp($a, $b) {
    global $sortfields;
    foreach ($sortfields as $sortfield) {
        $cmp = strcmp($a[$sortfield], $b[$sortfield]);
        // if desc, invert sign of $cmp
        if ($cmp !== 0)
            return $cmp;
    }
    return 0;
}

usort($all_rows, "cmp");

The function usort() calls a user defined comparison function, which returns the same logic from strcmp function: 0 if equal, < 0 is $a is less than $b and > 0 if $a is greater than $b.

This function will compare each field set in $sortfields variable, if it find any comparison that is different (in the order set), it will immediately return the difference.

1 Comment

What exactly does this do? Can you explain the logic of it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.