duanlaiquan8174 2015-04-08 09:26
浏览 112

如何在php中基于第二个数组更改数组位置

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
        )
)
  • 写回答

2条回答 默认 最新

  • duadlkc5762218 2015-04-08 09:36
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法