0

I have an array which i would like to sort it based on values from another one. FIrst array:

$array1 = ( [0]=> Int(2) 
    [1]=>Array(['id']=>String(5) , ['value']=>String(10))
    [2]=>Array(['id']=>String(5) , ['value']=>String(10))
    [3]=>Array(['id']=>String(5) , ['value']=>String(10))
)

And second one:

 $array2 = (1,4,3)

The result should be based on $array2:

 $array1 = ( [0]=> Int(2) 
    [1]=>Array(['id']=>String(5) , ['value']=>String(10))
    [3]=>Array(['id']=>String(5) , ['value']=>String(10))
    [2]=>Array(['id']=>String(5) , ['value']=>String(10))
)           

I've tried a bubble sort but does not work:

  for ($i = 1 ; $i <= $array1[0] ; $i++){
    for ($j = $i+1 ; $j <= $array1[0] ; $j++){
        if ($array2[$i] < $array2[$j]){
            $temp = $array1[$i];
            $array1[$i] = $array1[$j];
            $array1[$j] = $temp;  
        }
    }
}
2
  • 1
    Where did the 4 come from in $array2? Commented Jun 23, 2012 at 15:33
  • I think you need to explain yourself a little better. What do you mean that the result should be based on array 2? What exactly are you looking to do? In your bubble sort you're sorting by strings, comparing which one is alphabetically before the other and just using the Int in the position 0 of the array. What is that Int? The amount of entries in the array or what? Commented Jun 23, 2012 at 15:47

1 Answer 1

0

Have you tried checking array_multisort? You could inject array 2 into array 1 and then sort it that way?

1
  • could you be more specific ? what do you mean by inject ? show me an example. Commented Jun 24, 2012 at 10:23

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.