0

I want to change order of following array to 2nd array's values.

Array
(
    [2] => Array
        (
            [title] => Photometric Interpretation
            [name] => photometric_interpretation
        )

    [3] => Array
        (
            [title] => Make
            [name] => make
        )

    [4] => Array
        (
            [title] => Model
            [name] => model
        )

    [5] => Array
        (
            [title] => Strip Offsets
            [name] => strip_offsets
        )

    [6] => Array
        (
            [title] => Samples Per Pixel
            [name] => samples_per_pixel
        )

    [7] => Array
        (
            [title] => Rows Per Strip
            [name] => rows_per_strip
        )
)

I want to change order of above to following array's values.

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

What I have tried

$index = array_flip(['3,4,7,6,5,2']);
$assigned_fields = array_merge($fisrt_array, $index);

My desired output is

Array
(

    [3] => Array
        (
            [title] => Make
            [name] => make
        )

    [4] => Array
        (
            [title] => Model
            [name] => model
        )

    [7] => Array
        (
            [title] => Rows Per Strip
            [name] => rows_per_strip
        )

    [6] => Array
        (
            [title] => Samples Per Pixel
            [name] => samples_per_pixel
        )

    [5] => Array
        (
            [title] => Strip Offsets
            [name] => strip_offsets
        )

    [2] => Array
        (
            [title] => Photometric Interpretation
            [name] => photometric_interpretation
        )
)
Dev Hub
  • 301
  • 2
  • 9
  • 1
    Start with `array_multisort` – u_mulder Jun 22 '16 at 11:29
  • Possible duplicate of [Sort an Array by keys based on another Array?](http://stackoverflow.com/questions/348410/sort-an-array-by-keys-based-on-another-array) – kamal pal Jun 22 '16 at 11:37
  • @u_mulder you add some sample code? – Dev Hub Jun 22 '16 at 11:37
  • @DevHub the answer I've linked, works, check it out https://eval.in/593490 – kamal pal Jun 22 '16 at 11:50
  • @kamalpal in my question I have mentioned I have tried `array_merge` and in your marked link question, there is also `array_merge` solution which does not work. – Dev Hub Jun 22 '16 at 11:55
  • @DevHub solution with `array_replace` is also there in accepted answer. http://stackoverflow.com/questions/348410/sort-an-array-by-keys-based-on-another-array#9098675 – kamal pal Jun 22 '16 at 12:02

2 Answers2

2

This should work fine.

$a = ['2' => ['title' => 'Photometric Interpretation',
           'name' => 'photometric_interpretation'],
    '3' => ['title' => 'Make',
            'name' => 'make']];

$b = Array
(
    0 => 3,
    1 => 2
);

$c = [];
foreach($b as $s) {
    $c[$s] = $a[$s];
}
print_r($c);
phreakv6
  • 2,135
  • 1
  • 9
  • 11
1

You need to use array_replace instead of array_merge.

$assigned_fields = array_replace(array_flip($index), $fisrt_array);
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50