0

What I'd like to do is sort this multidimensional array by multiple keys. I've attempted to use array_multisort() but haven't had luck because of the type of array.

A function that can have multiple keys to it to sort by (relevancy, date) would be best so that other arrays with the same structure could use it as well.

This is the $array I'm working with:

Array
(
    [0] => DALQueryResult Object
        (
            [_results:DALQueryResult:private] => Array
                (
                    [0] => 32048
                    [id] => 32048
                    [1] => 1
                    [relevancy] => 1
                )
        )
    [1] => DALQueryResult Object
        (
            [_results:DALQueryResult:private] => Array
                (
                    [0] => 32002
                    [id] => 32002
                    [1] => 1
                    [relevancy] => 1
                )
        )
    [2] => DALQueryResult Object
        (
            [_results:DALQueryResult:private] => Array
                (
                    [0] => 31921
                    [id] => 31921
                    [1] => 1
                    [relevancy] => 1
                )
        )
    [3] => DALQueryResult Object
        (
            [_results:DALQueryResult:private] => Array
                (
                    [0] => 31868
                    [id] => 31868
                    [1] => 1
                    [relevancy] => 1
                )
        )
    [4] => DALQueryResult Object
        (
            [_results:DALQueryResult:private] => Array
                (
                    [0] => 31811
                    [id] => 31811
                    [1] => 1
                    [relevancy] => 1
                )
        )
)

I've tried this function (found here), but haven't had luck:

$sort = array();
foreach($array as $k=>$v) {
    $sort['relevancy'][$k] = $v['relevancy'];
    $sort['date'][$k] = $v['date'];
}
# sort by event_type desc and then title asc
array_multisort($sort['relevancy'], SORT_DESC, $sort['date'], SORT_DESC, $array);

After running array_multisort function I see this:

Fatal error: Cannot use object of type DALQueryResult as array
5
  • This is not answering your question but... DALQueryResult is part of a database class found on the webs, right? So two things really. Can't you sort them in your database query? Second, have a look at php's own PDO functions. they're practically the same thing, but offer more flexibility (Including getting records are arrays, or records as classes, like the ones in your post). Here's a nice tutorial... phpro.org/tutorials/Introduction-to-PHP-PDO.html Commented Apr 7, 2011 at 2:09
  • Jorg, Thanks for the answer. I'm using this as a test for something (code found on SmashingMag). Using ORDER BY is more time intensive in the query on a larger database. Correct me if I'm wrong but sorting the result in PHP would be faster than ORDER BY on a large result set... unfortunately sorting in PHP is far more of a pain though. Commented Apr 7, 2011 at 2:42
  • you can modify the code you found to not create a DALQueryResult object, but return an array. edit the wile loop to: while ($row = mysql_fetch_array($res)){ $results[] = $row; This will create an array of result arrays. maybe that will help using the multisort function. It will be less heavy on your db but heavier on your server. I don't know about speed... // small correction. Commented Apr 7, 2011 at 3:41
  • @Jorg Do you mean changing the whole WHILE loop to this? while ($row = mysql_fetch_array($res)){ $results[] = $row;} Or just removing $result = new DALQueryResult(); and replacing with $results[] = $row; ? Commented Apr 7, 2011 at 4:09
  • replacing while ($row = mysql_fetch_array($res)){ $result = new DALQueryResult(); foreach ($row as $k=>$v){ $result->$k = $v; } $results[] = $result; } return $results; with while ($row = mysql_fetch_array($res)){ $results[] = $row; } return $results; returns an array of arrays. Commented Apr 7, 2011 at 5:01

1 Answer 1

1

I take it you are using code that can be found here. If so, just modify your SQL query with:

ORDER BY relevancy, date

In your case it doesn't work because DALQueryResult is not an array, so $v['relevancy'] will cause an error (it should be $v->relevancy). Even if you changed it, it wouldn't work because DALQueryResult is an object, not an array (and it doesn't implement ArrayAccess either).

I'd also suggest you use PDO instead of a random class found on the web (if that's the case, of course). :)

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

6 Comments

Thanks for your answer. The reason why I'm looking for an alternate solution to using ORDER BY is because its a bigger strain on the database. By skipping ORDER BY in the query sorting in PHP the processing time would be faster. So if the function I've listed in my question doesn't work with this, is there another option to sort the array—without using PDO or ORDER BY?
@stwhite: MySQL has been built to handle tasks such as ordering result sets. It will be faster to sort in MySQL than PHP most of the times (if not all). Even faster if your fields are indexed. Not to mention query cache and scaling. You shouldn't worry about the database server load for such a trivial task.
@stwhite: If you really want to sort from a PHP object using array_multisort, you will have to have it implement ArrayAccess.
@Netcoder I'm checking out PDO right now (seeing lots of big advantages by using it). What I meant by 'strain on the database' is that it took a second longer to sort the mysql results using ORDER BY. The query uses Fulltext.
@stwhite: Do you have an index on the relevancy and/or date columns?
|

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.