0

I have got 2 arrays(One single and one multidimensional).

Single array "A" looks like

[questionid] => Array
        (
            [0] => 12
            [1] => 13
            [2] => 55
            [3] => 15
            [4] => 16
        )

Multidimensional array "B" looks like

Array
(
    [0] => Array
        (
            [quid] => 12
            [answer] => AAA
        )

    [1] => Array
        (
            [quid] => 13
            [answer] => neighbour
        )

    [2] => Array
        (
            [quid] => 15
            [answer] =>
        )

    [3] => Array
        (
            [quid] => 16
            [answer] =>
        )

     [4] => Array
        (
            [quid] => 55
            [answer] =>
        )
)

Now I want the array B (quid) values to be rearranged depending upon the values from array A. So in array B the value of quid last element(55) is at the very end whereas in array A it is in 3rd position.

I want the array B look like this

Array
(
    [0] => Array
        (
            [quid] => 12
            [answer] => AAA
        )

    [1] => Array
        (
            [quid] => 13
            [answer] => neighbour
        )

    [2] => Array
        (
            [quid] => 55
            [answer] =>
        )

    [3] => Array
        (
            [quid] => 15
            [answer] =>
        )

     [4] => Array
        (
            [quid] => 16
            [answer] =>
        )
)

The code for multidimensional array is

$ansid = array
(
     array
        (
            "quid" => 12,
            "answer" => "AAA"
        ),

     array
        (
            "quid" => 13,
            "answer" => "neighbour"
        ),

     array
        (
            "quid" => 15,
            "answer" =>""
        ),
     array
        (
            "quid" => 16,
            "answer" =>""
        ),

     array
        (
            "quid" => 55,
            "answer" =>""
        )
);

2 Answers 2

1

Not using array_walk() as to be mor demonstrative, you could just

$newB=array()
foreach ($arrayB as $b) $newB[$b['quid']]=$b;
$newA=array()
foreach ($arrayA as $k=>$v) $newA[$k]=$newB[$v]
//$newA has the required structure
2
  • Thanks but don't know why I am getting 'Warning: Illegal offset type' for multidimensional array.I am posting the structure in my question.
    – Raj
    Commented Aug 15, 2014 at 20:31
  • So stupid I am. Please ignore my last comment. Your solution is just awesome.Now I need to understand.Thank you so much for your help.
    – Raj
    Commented Aug 15, 2014 at 20:36
0

With the user sort function:

$single_array = ...; // order by the index of this array
$mult_dim_array = ...; // to be ordered by the 'quid' value of the elements

function my_comp($a, $b) {
    return array_search($a['quid'], $single_array ) - array_search($b['quid'], $single_array );
}

usort($mult_dim_array, "my_comp");

This will get the index on your first array to determine which element goes first or later. The function reads $single_array as a global variable (defined outside the function).

Documentation at http://php.net/manual/en/function.usort.php

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.