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
        )
)
6
  • 1
    Start with array_multisort Commented Jun 22, 2016 at 11:29
  • Possible duplicate of Sort an Array by keys based on another Array? Commented Jun 22, 2016 at 11:37
  • @u_mulder you add some sample code? Commented Jun 22, 2016 at 11:37
  • @DevHub the answer I've linked, works, check it out eval.in/593490 Commented Jun 22, 2016 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. Commented Jun 22, 2016 at 11:55

2 Answers 2

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

Comments

1

You need to use array_replace instead of array_merge.

$assigned_fields = array_replace(array_flip($index), $fisrt_array);

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.