wti
wti

Reputation: 484

PHP order array acording to another array values

I have two arrays, one has string elements and the other one has exactly the position where those elements are supposed to be. In the second array key represents a position and the value represents where the value from the first array is supposed to be. But I cant not manage make it work. This is the code that I have so far:

<?php
$array1= array("d","c","b","e","a");
$poss  = array (3  ,2  ,1  ,4  ,0);
var_dump(orderArray($poss, $array1));

function orderArray($poss, $data){
    $countPoss = count($poss);
    $countData = count($data);
    if ($countPoss === $countData){
        for ($i=0; $i<$countData; $i++){
            if($poss[$i]!==$i){
                $aux = $data[$i];
                $data = insertArrayIndex($data, $aux, $poss[$i]);
                unset($data[$i]);
                $data = array_values($data);
            }
        }
    }else{
        return FALSE;
    }
    return $data;
}
function insertArrayIndex($array, $new_element, $index) {
     $start = array_slice($array, 0, $index); 
     $end = array_slice($array, $index);
     $start[] = $new_element;
     return array_merge($start, $end);
 }

In other words, $array1 is supposed to be organized according to $poss. E.g.: the first element in $array1 'd' is supposed in in position 3 at the end, and 'a' is supposed to be in position 0. I've been trying hundreds of things, but I can not figure it out.

Upvotes: 1

Views: 63

Answers (4)

Ali
Ali

Reputation: 3081

All you need to do is to combine your arrays and then sort the result while maintaining the index association :)

// given
$array1= array("d","c","b","e","a");
$poss  = array (3  ,2  ,1  ,4  ,0);

// combine
$indexedArray = array_combine($poss,$array1);

// sort
asort($indexedArray);

// test
print_r($indexedArray);

which results to:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
)

Upvotes: 1

axiac
axiac

Reputation: 72216

If you program this as an exercise then I wish you patience and good luck!

But if you need this code in production then you better use the PHP sorting functions instead of reinventing the wheel.

Solution #1

Use array_combine() to create a new array having $poss as keys and $array1 as values then use ksort() to sort this new array using its keys:

$array1= array("d","c","b","e","a");
$poss  = array (3  ,2  ,1  ,4  ,0);

$out1 = array_combine($poss, $array1);
ksort($out1);
print_r($out1);

Solution #2

Use the function array_multisort() to sort $poss and $array1 on the same time. The first argument of array_multisort() tells the order, the values of the other arrays follow the movements of the values in the first array:

$array1= array("d","c","b","e","a");
$poss  = array (3  ,2  ,1  ,4  ,0);

array_multisort($poss, $array1);
print_r($array1);

The output of both methods:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
)

Upvotes: 1

splash58
splash58

Reputation: 26153

So you can get the array with correct indexes

$newarray = array();
foreach($poss as $key => $value)
  $newarray[$value] = $array1[$key];
var_dump($newarray);

And so with correct indexes and order

$newarray = array();
for($i = 0; $i < count($poss); $i++) {
  $newarray[] = $array1[array_search($i, $poss)];
}
var_dump($newarray);

Upvotes: 0

insanebits
insanebits

Reputation: 838

you can try using array_combine

// since keys are aligned with words you can combine into single array
$merged_array = array_combine($poss, $array1);
// then sort array by keys and your words are in correct position
ksort($merged_array);

// update $array1 with sorted values 
$array1 = array_values($merged_array);

Upvotes: 2

Related Questions