Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following two hypothetical arrays:

$values = array("fourth", "first", "second", "third");
$indices = array(3, 0, 1, 2);

What is the fastest way to rearrange $values based on $indices?

Is there perhaps some way to do $values->index_array = $indices?

share|improve this question
    
@Danack: when OP cannot express their thoughts - it's normal ) –  zerkms May 31 '13 at 1:27
1  
@zerkms your answer was correct and fast. Mine was correct but slower. Put it back and I'll upvote :) –  Danack May 31 '13 at 1:29
    
It would help if you showed what the desired result is. –  Barmar May 31 '13 at 1:34
add comment

3 Answers

up vote 0 down vote accepted

I'd do:

<?php
$newarray = array_fill_keys($indices, $values);
?>

This assumes your indices are always unique of course.

share|improve this answer
    
Thank you, that seems to be exactly what I needed –  TheWaterIsCold May 31 '13 at 1:22
    
I don't think this does what you want. It sets every element of $newarray to the same value. –  Barmar May 31 '13 at 1:26
    
It does do what he asked however. Is it perfect? Only the OP knows. –  Phillip May 31 '13 at 1:29
    
@bjørnSkagen: If this solved your problem, please mark it as the answer. –  Phillip May 31 '13 at 1:30
1  
This results in a 2-D array, where each element is a copy of $values, and the keys come from $indices. How is that "rearranging $values"? –  Barmar May 31 '13 at 1:31
show 3 more comments

It's just a guess based on a phrase from the question:

way to rearrange $values based on $indices

$values = array("fourth", "first", "second", "third"); $indices = array(3, 0, 1, 2);

array_multisort($indices, $values);

var_dump($values);

Online demo: http://ideone.com/UKfNiq

Output is:

array(4) {
  [0]=>
  string(5) "first"
  [1]=>
  string(6) "second"
  [2]=>
  string(5) "third"
  [3]=>
  string(6) "fourth"
}
share|improve this answer
    
This doesn't work. If any $indicies are out of order from the way PHP assigns array keys, the key value is lost. –  Phillip May 31 '13 at 1:34
    
@Phillip: it's OP's responsibility to guarantee they are in order. See: "rearrange $values based on $indices" –  zerkms May 31 '13 at 1:35
    
@zerms: Fair enough. This question sucks anyway. Time to be moving on. –  Phillip May 31 '13 at 1:37
add comment

ZerkMS's answer is correct as defined by the question but you may be better off taking another approach to generating the results.

If you can, rather than generating two arrays of:

$values = array("fourth", "first", "second", "third"); 
$indices = array(3, 0, 1, 2);

instead just generate one array and call ksort on it:

$valuesToSort = array();
$valuesToSort[3] = 'fourth';
$valuesToSort[0] = 'first';
$valuesToSort[1] = 'second';
$valuesToSort[2] = 'third';

ksort($valuesToSort);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.