John Royal
John Royal

Reputation: 371

PHP Re-order a numerically indexed array

I need to reorder a numerically indexed array arbitrarily. Take the following array which represent pages in a document:

$array[0  => 0
       1  => 1,
       2  => 2,
       3  => 3,
       4  => 4,
       5  => 5,
       6  => 6,
       7  => 7,
       8  => 8,
       9  => 9,
       10 => 10 ];

And a second array that defines the new order for specific elements (whilst automatically re-ordering those it would be conflicting with):

$reordered[3 => 4,
           7 => 9,
           6 => 1 ];

I need to have a resulting array that looks like:

$array[0  => 0
       1  => 2,
       2  => 3,
       3  => 4,
       4  => 5,
       5  => 6,
       6  => 1,
       7  => 9,
       8  => 7,
       9  => 8,
       10 => 10 ];

Note how where an element is being replaced, the others are re-indexed (whilst respecting the configuration of the $reordered array).

What is the most elegant way to achieve this?

Upvotes: 0

Views: 59

Answers (3)

Ihor Burlachenko
Ihor Burlachenko

Reputation: 4905

The following code does the job:

asort($reordered);
foreach ($reordered as $position => $item) {
    $sliced = array_splice($array, array_search($item, $array), 1);
    array_splice($array, $position, 0, $sliced);
}
print_r($array);

Outputs:

Array
(
    [0] => 0
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 1
    [7] => 9
    [8] => 7
    [9] => 8
    [10] => 10
)

Or you can use reorder function from Nspl. It has the following signature:

reorder(array $list, $from, $to)

The solution will be:

use function nspl\a\reorder;

asort($reordered);
foreach ($reordered as $position => $item) {
    $array = reorder($array, array_search($item, $array), $position);
}

Upvotes: 0

Monty Khanna
Monty Khanna

Reputation: 1120

Its working Try this :

$old = range(100,110);
$order = array(3=>4,6=>1,7=>9);

function match($old,$order){
    foreach ($old as $k => $v){
        if(array_key_exists($k, $order)){
            $new[] = $order[$k];
        }else{
            $new[] = $v;
        }
    }
    return $new;
}
echo "<pre>";
print_r($old);
print_r($order);
print_r(match($old,$order));

OUTPUT :

Array
(
    [0] => 100
    [1] => 101
    [2] => 102
    [3] => 103
    [4] => 104
    [5] => 105
    [6] => 106
    [7] => 107
    [8] => 108
    [9] => 109
    [10] => 110
)
Array
(
    [3] => 4
    [6] => 1
    [7] => 9
)
Array
(
    [0] => 100
    [1] => 101
    [2] => 102
    [3] => 4
    [4] => 104
    [5] => 105
    [6] => 1
    [7] => 9
    [8] => 108
    [9] => 109
    [10] => 110
)

Upvotes: 0

Vigneswaran S
Vigneswaran S

Reputation: 2094

try this : best way . I hope i solved your problem. use array_search($2, $array); gives 2 .to find and index to replace the value as given below.

<?php
$array=array(0=>0,1=>1,2=>2,3=>3,4=>4,5=>5,6=>6,7=>7,8=>8,9=>9);
$r=array(4=>2,5=>4,3=>6);
print_r($array);
echo "<br>";
$count=count($array);
foreach($r as $key =>$val){
$a=$array[$key];
$keyu = array_search($val, $array);
$array[$keyu]=$a;
$array[$key]=$val;
}
print_r($array);
?>

result:

Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 )
Array ( [0] => 0 [1] => 1 [2] => 5 [3] => 6 [4] => 2 [5] => 4 [6] => 3 [7] => 7 [8] => 8 [9] => 9 ) 

Upvotes: 0

Related Questions