1

I have 2 array. first:

$comment = Array ( 
    [1] => 1 
    [ 2] => 1 
    [ 3] => 8 
    [ 5] => 3 
    [ 6] => 2 
    [ 7] => 4 
         ) 

And I have a second array:

$item = Array ( 
[0] => Array ( [id] => 1 [title] => Neque porro quisquam est qui dolorem ) 
[1] => Array ( [id] => 2 [title] => Sed a est quis sem pellentesque luctus. ) 
[2] => Array ( [id] => 3 [title] => There is no one who loves pain itself ) 
[3] => Array ( [id] => 5 [title] => There is no one who loves pain itself ) 
[4] => Array ( [id] => 6 [title] => Sed a est quis sem pellentesque luctus. ) 
[5] => Array ( [id] => 7 [title] => Neque porro quisquam est qui dolorem ) 
) 

In the array "comment" key it is id in the array "item". I want to sort array "item" for the value of array "comment".

For Example:

[2] => Array ( [id] => 3 [title] => There is no one who loves pain itself ) // value in $comment 8
[5] => Array ( [id] => 7 [title] => Neque porro quisquam est qui dolorem )       // value in $comment 4
[3] => Array ( [id] => 5 [title] => There is no one who loves pain itself ) // value in $comment 3
...

I tried to sort using array_multisort but I could not do it. Help solve this problem.

1
  • 3
    I cannot see how you can sort $item using $comment to get your given result. Commented Jul 22, 2015 at 21:49

1 Answer 1

2

try this

// first merge the arrays

foreach ($item as $key => $tab) {
    $item[$key]["numComment"] = $comment[$tab["id"]];
}


// then sort

usort($item, function ($t1, $t2) {
    return $t2["numComment"] - $t1["numComment"];
});

var_dump($item);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I have a few hours of agony, you decide my problem.

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.