Php 会话数组的usort未按预期输出
我有一个img正方形的网格,可以使用可排序库将其拖入任何顺序。每个img都是mySQL db查询结果的可视化表示,该查询选择共享“imageparent”标识符的任何图像。它们在网格中的显示顺序取自数据库中的“imageorder”列,从0开始,依次工作,直到返回第n个图像 拖动img网格的目的是能够更改“imageorder”索引。拖动完成后,可排序库Php 会话数组的usort未按预期输出,php,arrays,sorting,session,usort,Php,Arrays,Sorting,Session,Usort,我有一个img正方形的网格,可以使用可排序库将其拖入任何顺序。每个img都是mySQL db查询结果的可视化表示,该查询选择共享“imageparent”标识符的任何图像。它们在网格中的显示顺序取自数据库中的“imageorder”列,从0开始,依次工作,直到返回第n个图像 拖动img网格的目的是能够更改“imageorder”索引。拖动完成后,可排序库通过ajax将一个“imageorder”变量发布到service.php,并被正确接收。因此,它发送的不是原始的0,1,2,3,4,5,6,7
通过ajax将一个“imageorder”变量发布到service.php
,并被正确接收。因此,它发送的不是原始的0,1,2,3,4,5,6,7顺序,而是类似于2,1,0,3,4,5,7,6的字符串。不难理解。切换顺序后,发送到service.php
的orderList
var总是正确的,但我最终发送到db并设置为会话var的数组在第二次或第三次拖动后顺序变得有点混乱,我不太清楚原因
代码示例和注释
$\u会话['selectedCsImages']数组结构:
[0] => Array
(
[imagename] => "Title"
[imageorder] => 0
[imageid] => 43
)
[1] => Array
(
[imagename] => "Title"
[imageorder] => 1
[imageid] => 21
)
[2] => Array
(
[imagename] => "Title"
[imageorder] => 2
[imageid] => 3
)
etc...
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Turn the orderList posted into an array
$removeChars = array('"','[',']');
$orderList = str_replace($removeChars, "", $_POST['order']); // POST received fine.
$listArray = explode(",",$orderList);
// Retrieve the session array
$sorting = $_SESSION['selectedCsImages'];
/* My logic is that I compare the $sorting array to $listArray and reorder $sorting by 'imageorder' to match $listarray */
usort($sorting, function($a, $b) use ($listArray) {
return array_search($a['imageorder'], $listArray) - array_search($b['imageorder'], $listArray);
});
/* I now have a $sorting array that (sometimes, hence the problem) matches the order that the images had just been dragged into by the user. Typically, as I mentioned above, it's correct after the first drag, but not always after the second or third where it creates a new order that I can't see a pattern or logic in. */
/* Had there not been errors with the usort function, I (would) have a $sorting array in the order I want but with imageorder values referring to pre-sorting. I iterate through the array and set each key to 0, 1, 2, etc. so that I have an array in the correct order and with each imageorder correctly stating its place.*/
$i = 0;
foreach ($sorting as $key => $value) {
$sorting[$key]['imageorder'] = $i;
$i++;
}
/* The information is attempted to be sent to the db and, on success I update the session var */
// Database code (runs succesfully and updates the db as per the image orders found in the $sorting array)
$_SESSION['selectedCsImages'] = $sorting;
Services.php摘录:
[0] => Array
(
[imagename] => "Title"
[imageorder] => 0
[imageid] => 43
)
[1] => Array
(
[imagename] => "Title"
[imageorder] => 1
[imageid] => 21
)
[2] => Array
(
[imagename] => "Title"
[imageorder] => 2
[imageid] => 3
)
etc...
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Turn the orderList posted into an array
$removeChars = array('"','[',']');
$orderList = str_replace($removeChars, "", $_POST['order']); // POST received fine.
$listArray = explode(",",$orderList);
// Retrieve the session array
$sorting = $_SESSION['selectedCsImages'];
/* My logic is that I compare the $sorting array to $listArray and reorder $sorting by 'imageorder' to match $listarray */
usort($sorting, function($a, $b) use ($listArray) {
return array_search($a['imageorder'], $listArray) - array_search($b['imageorder'], $listArray);
});
/* I now have a $sorting array that (sometimes, hence the problem) matches the order that the images had just been dragged into by the user. Typically, as I mentioned above, it's correct after the first drag, but not always after the second or third where it creates a new order that I can't see a pattern or logic in. */
/* Had there not been errors with the usort function, I (would) have a $sorting array in the order I want but with imageorder values referring to pre-sorting. I iterate through the array and set each key to 0, 1, 2, etc. so that I have an array in the correct order and with each imageorder correctly stating its place.*/
$i = 0;
foreach ($sorting as $key => $value) {
$sorting[$key]['imageorder'] = $i;
$i++;
}
/* The information is attempted to be sent to the db and, on success I update the session var */
// Database code (runs succesfully and updates the db as per the image orders found in the $sorting array)
$_SESSION['selectedCsImages'] = $sorting;
调试:
从调试来看,当我第二次或第三次从ajax调用此页面时,usort函数似乎发生了一些事情。这之后的一切都会按照预期的正确或错误顺序进行处理。sortable发布的orderList
var每次都是正确的。每次usort之后,我都会提供一个$sorted
变量的示例,但描述它就像上面的数组示例一样简单,在拖动之后,我没有指定顺序,而且我看不到它输出的看似随机的模式
通过研究,我认为在刷新页面之前保留会话变量是一个问题,但对services.php的ajax调用似乎应该刷新$\u会话['selectedCsImages']
var。我可能也读过这篇文章,我在不知不觉中使用了引用的数组值,并且——当我从一个会话变量到一个新数组并最终从这个数组保存回这个会话变量时——我可能已经创建了一些混乱的引用反馈。但是,我尝试使用$sorted=(array)clone(object)$\u会话['selectedCsImages']代码>在尝试usort之前,结果没有更改
PHP错误日志没有显示任何内容
更新:
根据@Ayaou的建议,我检查了$listArray
的输出,得到了一些意想不到的结果。我错误地认为,由于发布的$orderList
是正确的,所以分解的数组不是罪魁祸首
以下是完成16个img元素的以下订单交换后,print\r($listary)
的输出:第一个与第二个、第二个与最后一个、第六个与第七个:
1st and 2nd:
(
[0] => 1
[1] => 0
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[10] => 10
[11] => 11
[12] => 12
[13] => 13
[14] => 14
[15] => 15
)
last and 2nd last:
(
[0] => 1
[1] => 0
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[10] => 10
[11] => 11
[12] => 12
[13] => 13
[14] => 15
[15] => 14
)
6th with 7th:
(
[0] => 1
[1] => 0
[2] => 2
[3] => 3
[4] => 4
[5] => 6
[6] => 5
[7] => 7
[8] => 8
[9] => 9
[10] => 10
[11] => 11
[12] => 12
[13] => 13
[14] => 15
[15] => 14
)
我的想法是,$listary
每次都会显示一个连续的0、1、2、3等,只有两个交换的项目显示订单变化。事实并非如此,我将再次查看$orderList,并检查我的可排序库是否正在更新它从更新的会话变量中正确获取的订单。旧的订单交换将保留在链的某个不应该保留的地方。解决方案在您的可排序
表单(前端)上,因此,与其发送订单
post数据上的图像订单
,不如发送图像ID
索引
然后像这样更改排序回调
//Use imageid index instead of imageorder
usort($sorting, function($a, $b) use ($listArray) {
return array_search($a['imageid'], $listArray) - array_search($b['imageid'], $listArray);
});
你能发布$listary
的内容吗?建议你对排序部分做一些单独的测试(一个脚本只包含这些内容,加上$listary内容的静态变化-检查结果是否与你对不同情况的期望相匹配)-不确定这是否真的能够实现你想要的。谢谢@CBroe,就在我写问题之前,我一直在尝试,现在还在努力@阿亚欧,你可能有什么发现,干杯。我认为,$listary
将是罪魁祸首,因为它爆炸了我检查过的字符串,这是正确的。然而,它似乎产生了意想不到的输出。我会更新问题的。就是这样,谢谢你。这是一个我可能浪费了太多时间得出的结论,因为由于整个前端和后端都来自于我以前编写的工作完美的代码,我对可排序的一面感到自满。我所改变的只是数据库过程,一路上有些东西乱七八糟。事实证明,我一直在使用图像顺序作为每个图像的id。但你的直觉是正确的,正是它解决了问题。