用php更新排序?

用php更新排序?,php,Php,我正在尝试用PHP实现一个图像库管理删除过程。我一直在琢磨如何更新列表顺序,以便在删除后它们保持相同的顺序 我有一个关联数组($images),其中键与“order”值相同。这个数字定义了库中的位置。我还有一个应该删除的订单号列表。通过使用订单号标识来删除每个图像 $images format array(28) { [1]=> array(5) { ["gallery_id"]=> string(2) "71" ["property_id"]=>

我正在尝试用PHP实现一个图像库管理删除过程。我一直在琢磨如何更新列表顺序,以便在删除后它们保持相同的顺序

我有一个关联数组($images),其中键与“order”值相同。这个数字定义了库中的位置。我还有一个应该删除的订单号列表。通过使用订单号标识来删除每个图像

$images format

array(28) {
[1]=>
  array(5) {
    ["gallery_id"]=>
    string(2) "71"
    ["property_id"]=>
    string(1) "3"
    ["picture"]=>
    string(17) "imgname.jpg"
    ["order"]=>
    string(1) "1"
    ["alt_text"]=>
    string(14) "discription"
  }
[2]=>
  array(5) {
    ["gallery_id"]=>
    string(2) "83"
    ["property_id"]=>
    string(1) "3"
    ["picture"]=>
    string(17) "imgname.jpg"
    ["order"]=>
    string(1) "2"
    ["alt_text"]=>
    string(14) "discription"
  }
So on... how ever large the list might be.
要删除的图像列表

$removedImgs

array(2) {
    [0]=> string(1) "1"
    [1]=> string(1) "3"
}
上图显示将从库中删除图像1和3

Current:    1 2 3 4 5 6 ...
Removal:    2 4 5 6
            | | | |
Reordering: 1 2 3 4
实际删除代码

// Loop though with each image and remove the ones posted from the list
foreach ($_POST['orderID'] as $removeImg)
{
    // Store each removed images order id
    $removedImgs[] = $removeImg;

    // If we're removing images create a list of the image paths to
    // unlink the files later.
    if (isset($images[$removeImg]))
    {
        $unlinkList[] = $imgPath . $images[$removeImg]['picture'];
        $unlinkList[] = $imgPath . 'thumbs/thumb' . $images[$removeImg]['picture'];
    }

    // $images should only contain the ones that we haven't removed.
    unset($images[$removeImg]);

    // Update the image order
    foreach ($images as $key => &$img)
    {
        if ($key > $removeImg)
        {
            (int)$img['order']--;
        }
    }
    var_dump($images);
    echo "\n\n==========\n\n";
}

如果您可以控制何时删除图像,那么您将更容易在那时更新订单

function removeImage($images, $imgName)
{
    $removedImgNum = $images[$imgName]['order'];
    $images[$imgName] = undefined; // or delete, etc

    foreach ($images as $img)
    {
        if ($img['order'] > $removedImgNum)
            $img['order']--;
    }
}

我不确定我是否理解,但也许将这两个数组与array\u diff\u assoc()进行比较,然后使用ksort()对结果进行排序可以满足您的需要。

我确实想到了这一点,但我想看看是否有更好的方法,而不是每次都在列表中循环。我刚刚实现了这一点,但我想看看我是否能想出一个更好的选择,并认为我用removedCounter变量实现了它。谢谢,如果我真的想到一个更好的方法来浏览列表,我将把它作为一个解决方案发布在这里。