gpt4 book ai didi

用于更改数组的 "order"的 PHP 算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:08:15 24 4
gpt4 key购买 nike

场景:

1) 我有一个由菜单项组成的菜单,每个菜单项都有一个 order_index(范围从 0 到 n),它标识菜单项在菜单中的位置。
2) 我想在特定位置插入一个新的菜单项或更改现有元素的位置。
3) 假设如果它是一个新项目,我已经用 order_index = null 将新项目保存在我的数据库中。
4) 因此,当编辑/创建菜单项时,除了 order_index 之外的所有相关信息都会被获取、保存,然后调用以下函数:

function reOrderItems(MenuItem $item, Integer $orderIndex)
{
/*
Step 1: Retrieve all the MenuItems, ordered by order_index. This means that
If it was a new item, the item's order_index is null and would be the
first item in the array of retrieved items.

Step 2: Take the item ($item) and remove it from its current location in the
array and place it at its new position, $orderIndex.

Step 3: "Reorder" the array indexing so that it runs from 0 to
(array.length - 1) in the order that the Menu Items are now.

Step 4: Update all items in the database with their new order_index, ranging
from 0 to n according to the array index.
*/
}

示例 1:将位置 [0] 中的项目移动到位置 [3]。[1] => [0], [2] => [1], [3] => [2], [0] => [3],所有其他元素保持不变。

示例 2:将位置 [6] 中的项目移动到位置 [3]。位置 [0]、[1] 和 [2] 中的项目保持不变。 [3] => [4], [4] => [5], [5] => [6], [6] => [3], 所有其他元素保持不变。

对于能够为我完成第 2 步和第 3 步的算法,我将不胜感激。请记住,真正的天才在于简单。实现此目的的算法越简单越好。

最佳答案

您可以使用内置的 PHP 函数 array_merge()array_splice() .我认为它回答了你的问题,但表的索引可能会通过使用这些函数重建,我不确定这对你来说是否是个问题。

<?php

$menu = array(
0 => 'Item 1',
1 => 'Item 2',
2 => 'Item 3',
3 => 'Item 4',
4 => 'Item 5'
);

echo('Default menu:'.PHP_EOL);
print_r($menu);

$menuNewEntry = array(
0 => 'Item 0 (new)'
);

##### add a menu entry at the beginning #####
$menu = array_merge($menuNewEntry, $menu);

echo('Default menu with new entry:'.PHP_EOL);
print_r($menu);

##### remove a menu entry #####
array_splice($menu, 2, 1);

echo('Default menu without entry at index 2:'.PHP_EOL);
print_r($menu);

##### move a menu entry #####
# remove an element from the array and keep the result (the removed element)
$menuRemovedEntry = array_splice($menu, 0, 1);

# insert this element in the array (the removed element)
array_splice($menu, 2, 0, $menuRemovedEntry);

echo('Default menu with entry 0 moved at index 2:'.PHP_EOL);
print_r($menu);

结果如下:

Default menu:
Array
(
[0] => Item 1
[1] => Item 2
[2] => Item 3
[3] => Item 4
[4] => Item 5
)
Default menu with new entry:
Array
(
[0] => Item 0 (new)
[1] => Item 1
[2] => Item 2
[3] => Item 3
[4] => Item 4
[5] => Item 5
)
Default menu without entry at index 2:
Array
(
[0] => Item 0 (new)
[1] => Item 1
[2] => Item 3
[3] => Item 4
[4] => Item 5
)
Default menu with entry 0 moved at index 2:
Array
(
[0] => Item 1
[1] => Item 3
[2] => Item 0 (new)
[3] => Item 4
[4] => Item 5
)

关于用于更改数组的 "order"的 PHP 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21256433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com