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
Community
  • 1
  • 1
stwhite
  • 3,156
  • 4
  • 37
  • 70
  • 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... http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html – Jorg Apr 07 '11 at 02: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. – stwhite Apr 07 '11 at 02: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. – Jorg Apr 07 '11 at 03: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; ? – stwhite Apr 07 '11 at 04: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. – Jorg Apr 07 '11 at 05:01

1 Answers1

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). :)

netcoder
  • 66,435
  • 19
  • 125
  • 142
  • 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 Apr 07 '11 at 02:39
  • @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. – netcoder Apr 07 '11 at 02:52
  • @stwhite: If you really want to sort from a PHP object using `array_multisort`, you will have to have it implement [ArrayAccess](http://php.net/ArrayAccess). – netcoder Apr 07 '11 at 02:53
  • @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 Apr 07 '11 at 03:33
  • @stwhite: Do you have an index on the `relevancy` and/or `date` columns? – netcoder Apr 07 '11 at 04:02
  • @Netcoder I do have an index on date, but is it possible to have an index on relevancy since it comes from: MATCH(p.col1, p.col2, p.col3) AGAINST ('$query' IN BOOLEAN MODE) as relevancy ? – stwhite Apr 07 '11 at 04:15