如何搜索和重置我的以下数组?

时间:2017-02-16 10:44:59

标签: php arrays sorting multidimensional-array

Actully array1是我的默认数组。但我提供从我的网站购买阅读订单的设施。现在,如果某个casino_id 78是购买阅读订单1而不是我必须将其保持在第一位置。它的实际读数是4。 考虑readingOrder = rank和id = casino_id。

所以基本上array1是我的默认数组.array2是采购订单,我希望结果从这两个数组中得到array3。

$array1 = Array
        (
        [0] => Array
            (
                [id] => 76
                [readingOrder] => 1
            )

        [1] => Array
            (
                [id] => 81
                [readingOrder] => 2
            )

        [2] => Array
            (
                [id] => 78
                [readingOrder] => 3
            )

        [3] => Array
            (
                [id] => 80
                [readingOrder] => 4
            )

        [4] => Array
            (
                [id] => 82
                [readingOrder] => 5
            )

        [5] => Array
            (
                [id] => 83
                [readingOrder] => 6
            )

        [6] => Array
            (
                [id] => 84
                [readingOrder] => 7
            )

        [7] => Array
            (
                [id] => 85
                [readingOrder] => 8
            )

        [8] => Array
            (
                [id] => 86
                [readingOrder] => 9
            )

        [9] => Array
            (
                [id] => 87
                [readingOrder] => 10
            )

    )

Secon数组: -

$array2 = Array
(
    [0] => Array
        (
            [rank] => 1
            [casino_id] => 78
        )

    [1] => Array
        (
            [rank] => 4
            [casino_id] => 76
        )

)
第三个: -

$array3 = Array
    (
        [0] => Array
            (
                [id] => 78
                [readingOrder] => 1
            )


        [1] => Array
            (
                [id] => 81
                [readingOrder] => 2
            )


        [2] => Array
            (
                [id] => 80
                [readingOrder] => 3
            )

        [3] => Array
            (
                [id] => 76
                [readingOrder] => 4
            )


        [4] => Array
            (
                [id] => 82
                [readingOrder] => 5
            )

        [5] => Array
            (
                [id] => 83
                [readingOrder] => 6
            )

        [6] => Array
            (
                [id] => 84
                [readingOrder] => 7
            )

        [7] => Array
            (
                [id] => 85
                [readingOrder] => 8
            )

        [8] => Array
            (
                [id] => 86
                [readingOrder] => 9
            )

        [9] => Array
            (
                [id] => 87
                [readingOrder] => 10
            )

    )

1 个答案:

答案 0 :(得分:1)

对于array_combinearray_column混合的情况,这似乎适用于您的情况。查看内联评论以获取详细信息。

// get (rank => casino_id) type array from array2 using array functions.
$array3 = array_combine(array_column($array2, 'rank'), array_column($array2, 'casino_id'));
//similar type from array1
$array4 = array_combine(array_column($array1, 'readingOrder'), array_column($array1, 'id'));

$temp = $finalArr = array();
//push only if the value is present in high priority array (array3).
foreach ($array4 as $key => $val) {
  $temp[$key] = $array3[$key] ? $array3[$key] : $array4[$key];
}

//finally modify the array according to original key=> value type.
foreach ($temp as $k => $v) {
  $finalArr[] = array(
    'id' => $v,
    'readingOrder' => $k,
  );
}