2015-04-08 3 views
1

У меня есть два многомерных массива, и я хочу изменить позицию элемента массива на основе второго массива. Я никогда не делал этого, так что не знаю, как это сделать с массивом. Здесь я отправляю результат массива.Как изменить позицию массива на основе второго массива в php

Первый (основной) массив:

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 
     ) 
) 

Второй массив:

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 
     ) 
) 

Я хочу, чтобы этот результат массива:

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 
     ) 
) 
+1

даже не попытка Еогеасп? – Ghost

+0

Извините, но я не знаю, как это сделать :( –

+1

@ Mr.Happy Вы не можете застрять до тех пор, пока вы не попробуете что-нибудь! – Rizier123

ответ

0

, если я правильно понял - вы должны найти отличия между первым и вторым, а затем добавить, что элементы в массив второго

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

Работает ли, если оба ключа массива отличаются? –

1

Один из способов будут просто петля как массивы и контейнер. В цикле, если ваш конкретный ключ совпадает, поместите элементы в их первое, что соответствует (сортировка фильтрации), затем, после этого, объедините остальные.

$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

Смежные вопросы