弹出并从一个关联数组推送到另一个数组

时间:2017-02-17 10:44:24

标签: php arrays sorting arraylist

我想从array2弹出并想要推入array1。 但根据一些自定义要求。

现在在array1中,第一个键的readOrder为1,第二个键的readOrder为4。

所以我想在array2的前两个键之间推送这两个键。对于所有其他键,这个过程都是相同的。

我的最后一个数组必须像array3一样。

例如在array1 key [0]中readOrder为1而key [1]为4.现在我想从array2中再推两个键。 对于array1键[2]读取顺序是6.所以在此键之前我想从array2推送另一个键,并且相同以进一步...

array1如下所示

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

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

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

)

array2如下所示

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

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

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

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

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

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

    [6] => Array
        (
            [id] => 88
            [readingOrder] => 11
        )

)

输出数组3:

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

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

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

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

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

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

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

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

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

    [9] => Array
        (
            [id] => 88
            [readingOrder] => 11
        )

)

谢谢..

2 个答案:

答案 0 :(得分:1)

您可以像这样构建数组:

frmPass pass = new frmPass(lblAID.Text, lblName.Text, lblType.Text, "User Module");
DialogResult dr = pass.ShowDialog();

if (dr == DialogResult.Ok)
{
    //do code if form is closed with OK dialog result
}
else
{
    // some warning to user...
}

请注意,此代码对$current = 1; $arr3 = []; while ( $arr1 && $arr2 ) { if ( $arr1[0]['readingOrder'] > $current ) $arr3[] = array_shift($arr2); else $arr3[] = array_shift($arr1); $current++; } $arr3 = array_merge($arr3, $arr1, $arr2); print_r($arr3); $arr1具有破坏性。如果要保留它们,请先复制它们,然后使用副本。

答案 1 :(得分:0)

您可以使用usort执行此操作。首先,您需要合并数组:

$a1 = [
    [
        'readingOrder' => 1,
        'id' => 78
    ],
    [
        'readingOrder' => 4,
         'id' => 76
    ],
    [
        'readingOrder' => 6,
        'id' => 80
    ]
];
$a2 = [
    [
        'readingOrder' => 2,
        'id' => 81
    ],
    [
        'readingOrder' => 5,
        'id' => 82
    ],
    [
        'readingOrder' => 7,
        'id' => 84
    ],
    [
        'readingOrder' => 8,
        'id' => 85
    ]
];

$a3 = array_merge($a1, $a2);

然后你需要使用usort

usort($a3, function($a,$b) {
    if ($a['readingOrder'] == $b['readingOrder']) return 0;
    return $a['readingOrder'] < $b['readingOrder'] ? -1 : 1;
});

在PHP 7中,您现在可以使用太空船操作员,这将使代码更加干净。像这样:

usort($a3, function($a,$b) {
    return $a[0] <=> $b[0];
});

然后返回:

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

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

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

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

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

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

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

)