Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort array preserving its initial state

Tags:

php

cakephp

Using CakePHP 2.0 here. The title is a bit misleading, so to clear things up - I have session array which is populated with ids of products as user adds them to cart. So this array goes like [0] => 25, [1] => 70 etc. Let's say this array is called $products.

What I want to ask is is there some possibility to have array which I get by using 'find' model's function ('conditions' => array('Product.id' => $products)) to be sorted not by some of Model.field values (as in 'order' option), but by $products array indices, so that when I render the products content in view I would get all those products in cart sorted in such sequence as user were adding them.

Here's an example - Session array $products:

[0] => 35,
[1] => 15,
[2] => 25

Then I pass this array to conditions of find function:

$list = $this->Product->find('all', array('conditions' => array('Product.id' => $products)));

In the end $list gives me array which sorted by product.id. So instead of having:

[0] => Array
    (
        [Product] => Array
            (
                [id] => 35 )),
[1] => Array
        (
            [Product] => Array
                (
                    [id] => 15 )),

[2] => Array
        (
            [Product] => Array
                (
                    [id] => 25 ))

I get:

[0] => Array
    (
        [Product] => Array
            (
                [id] => 15 )),
[1] => Array
        (
            [Product] => Array
                (
                    [id] => 25 )),
[2] => Array
        (
            [Product] => Array
                (
                    [id] => 35 ))

So far all the answers doesn't solve my problem. Please, pay closer attention to the example I gave, it's very simple, but the answers provided are in different key.

Again, I need final array $list to be sorted by indices of session array $products, but I get $list sorted by Product.id field, even though I didn't specify any 'find(array('order'=> ...))' at all and even tried to set it to false.

like image 720
kK-Storm Avatar asked Apr 19 '12 13:04

kK-Storm


People also ask

How do you sort an array without creating a new array?

To sort an array, without mutating the original array:Call the sort() method on the copied array. The sort method will sort the copied array, without mutating the original.

Is sort () destructive?

sort() is a destructive process that sorts the original list in place. Note that reverse() returns None . By default, the list is sorted in ascending order.

How do you sort an array in a specific order?

sort((a,b) => { if ( a.name < b.name ){ return -1; } if ( a.name > b.name ){ return 1; } return 0; }); And it is trivial enough to swap the sort order by switching the returns or the if statements above.

Does sort mutate array javascript?

The sort() method returns a reference to the original array, so mutating the returned array will mutate the original array as well.


1 Answers

Don't want to sound rude, but all of responses to my question were about how to order my array by Product id (which was clearly not what I asked) or suggestions on doing manual sql queries which is inappropriate. Seems like I couldn't properly describe what I needed, but I tried to make it as clear as possible, sorry.

I stumbled upon a solution while trying to find how to 'disable order' in cake. So basically what you need to do is to have an array (let's call it $queue) where you keep product ids as they are added to cart, and then in 'order' option in find model function (or paginate) you should provide it like:

'order'=>array('FIELD(Product.id,'.implode(',', $queue).')')

$queue might look like this, as example:

[queue] => Array
        (
            [0] => 51
            [1] => 1
            [2] => 11
        )

In the end you get $list array in which the order of products is same as in $queue. If you didn't specify 'order' then you would get you $list array like this:

 [queue] => Array
            (
                [0] => 1
                [1] => 11
                [2] => 51
            )
like image 121
kK-Storm Avatar answered Oct 01 '22 18:10

kK-Storm