1

I have two multidimensional array and I want to change array element position based on second array. I have never done this way so no idea how to do this with array. Here I am posting my array result.

First(main) array:

Array
(
    [0] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1201
        )

    [1] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1200
        )

    [2] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1196
        )

    [3] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1193
        )

    [4] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1191
        )

    [5] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1145
        )

    [6] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1144
        )

    [7] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1139
        )

    [8] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1135
        )

    [9] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1053
        )
)

Second array:

Array
(
    [0] => stdClass Object
        (
            [bounced_id] => 2
            [user_id] => 156
            [property_id] => 1193
        )

    [1] => stdClass Object
        (
            [bounced_id] => 1
            [user_id] => 156
            [property_id] => 1191
        )

    [2] => stdClass Object
        (
            [bounced_id] => 26
            [user_id] => 156
            [property_id] => 1200
        )
)

I want this array results:

Array
(
    [0] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1193
        )

    [1] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1191
        )

    [2] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1200
        )


    [3] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1201
        )

    [4] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1196
        )

    [5] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1145
        )

    [6] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1144
        )

    [7] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1139
        )

    [8] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1135
        )

    [9] => stdClass Object
        (
            [user_type] => U
            [user_id] => 156
            [property_id] => 1053
        )
)
6
  • 1
    not even a foreach attempt? Commented Apr 8, 2015 at 9:32
  • Sorry but I dont no how to do this :( Commented Apr 8, 2015 at 9:34
  • 1
    @Mr.Happy You can't get stuck as long as you don't try something! Commented Apr 8, 2015 at 9:35
  • can you post your array with code format Commented Apr 8, 2015 at 9:35
  • @User This array results I am geeting from sql result. Commented Apr 8, 2015 at 9:40

2 Answers 2

1

One way would be just to loop both arrays and a container. Under the loop, if your particular key matches, put the items their first that matches (sort of filtering), then after thats done, merge the rest.

$result = array(); // container
foreach($array2 as $k2 => $val2) {
    foreach($array1 as $k1 => $val1) {
        if($val2->property_id == $val1->property_id) { // if it matches
            $result[] = $val1; // put it inside
            unset($array2[$k2], $array1[$k1]); // remove to continue next set
            break;
        }
    }
}
$result = array_merge($result, $array1); // merge the rest

Sample Output

Sign up to request clarification or add additional context in comments.

Comments

0

if I understood you correctly - you need to find differencies between first and second, and then append that elements to second array

$array_diff = array_diff($first,$second);
$array_result = array_merge($second, $array_diff);

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.