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 a problem in php, i want to re-index the value of an array according to their key in order to make each array have the same order to be able to easily compare them (to remove duplicates) and it should apply to any length of array remove duplicates.

For example A,C,B,D / D,B,C,A / C,A,B,D should ALL be be reordered to have the value A,B,C,D because the given list of character with their index is 0->A , 1->B, 2->C, 3->D

for instance let's say we have this array

Array(
  [0] => Array (
    [0] => S,
    [1] => E,
    [2] => Q,
    [3] => A
  )
) 

and we have a given list of character with indexes( the character with the index they should have):

// index --> character ( array with real indexes)
$array_real_index = Array(
  [0] => A, 
  [1] => C, 
  [2] => D, 
  [3] => B, 
  [4] => E, 
  [5] => Q, 
  [6] => R,
  [7] => S
)

If character found index should be replace (if not the right one) with the correct one.

So the result should be:

Array (
  [0] => A, 
  [1] => E, 
  [2] => Q, 
  [3] => S
) 

Thanks for your future help.

share|improve this question
    
I think I reformatted that without any meaning loss, but your structure was a bit confusing (parenthesis and bracket matching). Please do a read-through and make sure I've formatted it correctly. –  Brad Christie Mar 11 '13 at 15:40
    
Would simply sortng both arrays not be sufficient? –  Captain Payalytic Mar 11 '13 at 15:42
    
You can search SO questions, it was asked so many times. Use a usort function or similar. –  Voitcus Mar 11 '13 at 15:42
add comment

3 Answers

Try this:

<?
  $sorted_array = array_intersect($sort_array, $array_real_index);
  sort($sorted_array);
  print_r($sorted_array);
?>
share|improve this answer
    
thanks, could you please explain. –  StringerB Mar 11 '13 at 16:39
    
tried it didn't work. –  StringerB Mar 11 '13 at 16:47
add comment
  1. Custom sort means one of the usort() family of functions.
  2. Custom filter means array_filter().

Code:

$master = array('A', 'C', 'D', 'B', 'E', 'Q', 'R', 'S');

function myCompare($a, $b) {
  global $master;
  $index_a = array_search($a, $master, TRUE);
  $index_b = array_search($b, $master, TRUE);
  if( $index_a === false && $index_b === false ) {
    // if neither are in the array, then sort by their normal value
    return ($a === $b) ? 0 : ($a < $b) ? -1 : 1;
  } else if( $index_a === false ) {
    // if only one is in the array, then it is "greater" than the other
    return 1;
  } else if( $index_b === false ) {
    return -1;
  } else {
    // otherwise the item with the lowest index is the "greater" of the two
    return ($index_a === $index_b) ? 0 : ($index_a < $index_b) ? -1 : 1;
  }
}

function myFilter($var) {
  global $master;
  return in_array($var, $master);
}

$input = array('A','B','C','D','E','F','G','H');
$input = array_filter($input, 'myFilter');
usort($input, 'myCompare');
print_r($input);

/* Output:
Array
(
    [0] => A
    [1] => C
    [2] => D
    [3] => B
    [4] => E
) */
share|improve this answer
    
What does that means return ($a === $b) ? 0 : ($a < $b) ? -1 : 1; ?? –  StringerB Mar 11 '13 at 16:26
    
and why do you have a master in your array_search. if you could explain the argument in your array_search it would be helpful. thanks –  StringerB Mar 11 '13 at 16:27
    
if($a===$b){ return 0; } else { if($a<$b) { return -1; } else { return 1; } }, but using the ternary operator. And the $master variable is declared at the top of the code block, and is your sort order/filter set. –  Sammitch Mar 11 '13 at 16:33
add comment
$original_array = Array(
    0 => 'S',
    1 => 'E',
    2 => 'Q',
    3 => 'A'
);
$array_real_index = Array(
  0 => 'A', 
  1 => 'C', 
  2 => 'D', 
  3 => 'B', 
  4 => 'E', 
  5 => 'Q', 
  6 => 'R',
  7 => 'S'
);

// Custom sorting function passed to usort. $a & $b are two values
// being compared in terms of how they should be sorted. Results:
// 1:  $a > $b  (move $b down one and $a up one)
// -1: $b > $a  (move $a down one and $b up one)
// 0:  $a == $b (no change)
function sort_using_real_index($a,$b){
  // allow access to $array_real_index from within this function
  global $array_real_index;

  // retrieve the key indexes of the values. E.g. $a might contain
  // 'R', array_search would return 6. 
  $aI = array_search($a, $array_real_index);
  $bI = array_search($b, $array_real_index);

  // Both the found keys are then compared and returned
  // (1, -1 and 0 results reflect how $a and $b relate in terms
  // of sort order within the sorted result)
  return $aI == $bI
    ? 0
    : ($aI > $bI ? 1 : -1);
}

// dump out the original array (benchmark)
var_dump($original_array);

// sort the original array using the custom function
usort($original_array, 'sort_using_real_index');

// output the sorted result
var_dump($original_array);

example

As others said, you can use usort and pass a custom function that uses the keys from one array as the comparer in the sort function.

Wanted to use usort to demonstrate how you can sort arrays using whatever metric you find most useful.

share|improve this answer
    
Seems to be working on the 'example' thanks. Could you actually explain your code in words because there are some parts which I don't get. Thanks –  StringerB Mar 11 '13 at 16:07
    
@StringerB: See my updated answer for inline comments. –  Brad Christie Mar 11 '13 at 16:36
    
ok thanks. Tried but not working, when I use the function sort_using_real_index($a,$b){. what should i feed $a and $b with, what are they meant to be. @BradChristie –  StringerB Mar 11 '13 at 17:32
    
@StringerB: that function is meant to be called only by usort, not directly. that's why the name of the function is passed as a string to usort. –  Brad Christie Mar 11 '13 at 19:59
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.