会话数组的输出未按预期输出

时间:2017-02-21 11:03:16

标签: php arrays sorting session usort

我有一个img方格网格,可以使用可排序库拖动到任何顺序。每个img都是mySQL数据库查询结果的直观表示,该查询选择共享'imageparent'标识符的任何图像。它们在网格中显示的顺序取自数据库中的'imageorder'列,从0开始,按顺序工作,直到返回的第n个图像数。

拖动img网格的目的是能够更改'imageorder'索引。完成拖动后,可排序的库POSTS'imageorder' var由ajax到service.php并正确接收。因此,它不是原始的 0,1,2,3,4,5,6,7 顺序,而是发送一个像 2,1,0,3,4的字符串, 5,7,6 。不太难掌握。切换顺序后,发送到orderList的{​​{1}} var始终是正确的,但是数组我最终发送到数据库并设置为我的会话var在第二个或第三个之后按顺序变得有点乱码拖,我不太清楚为什么。

代码示例和注释

$ _ SESSION ['selectedCsImages']数组结构:

service.php

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...

调试:

从调试开始,当我第二次或第三次从ajax调用此页面时,似乎usort函数出现了问题。此后的所有内容都会通过罚款并根据预期处理正确或错误的订单。 sortable 发布的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; var每次都是正确的。我每次都会提供orderList var的样本,但是按照我在拖动后未指定的顺序将其描述为上面的数组示例一样简单,我无法在看似随意的顺序输出。

从研究开始,我认为在页面刷新之前保留会话变量是一个问题,但似乎对services.php的ajax调用应刷新$sorted var。我也读过,也许,我在不知不觉中使用了引用的数组值 - 当我从一个会话var到一个新数组,并最终从这个数组保存回这个会话变量时 - 我可能已经创建了一些混乱的参考反馈。但是,我在尝试使用之前尝试使用$_SESSION['selectedCsImages'],结果没有改变。

PHP错误日志没有显示任何内容。

更新

根据@Ayaou的建议,我检查了$sorted = (array)clone(object)$_SESSION['selectedCsImages'];的输出并得到了一些意想不到的结果。我错误地认为,由于发布的$listArray是正确的,爆炸阵列不会是罪魁祸首。

以下是完成以下16个img元素的下订单交换后$orderList的输出:第1个带第2个,第2个带有最后一个,第6个带有第7个:

print_r($listArray)

我正在进步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 ) 将显示顺序0,1,2,3等的想法。每次只有两个交换的项目显示订单更改。事实并非如此,我将再次回顾$ orderList并检查我的可排序库是否正在更新它从更新的会话var中正确获取的订单。较旧的订单掉期被保留在链条的某个地方,而不是它们。

1 个答案:

答案 0 :(得分:2)

解决方案位于sortable表单(位于前端),因此,不要在imageorder发布数据上发送'order',而是发送imageid索引。

然后像这样改变你的排序回调

//Use imageid index instead of imageorder
usort($sorting, function($a, $b) use ($listArray) {
        return array_search($a['imageid'], $listArray) - array_search($b['imageid'], $listArray);
    });