1

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.

3
  • 1
    What is the expected outcome? Commented Jul 10, 2015 at 12:14
  • Not sure if this is what you're looking for but you can do something like this: $new_array = array_combine($poss, $array1); asort($new_array); Commented Jul 10, 2015 at 12:15
  • Did you get your answer? If not post your expected result Commented Jul 10, 2015 at 12:31

4 Answers 4

2

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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
)

Comments

1

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
)

Comments

0

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);

Comments

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.