简体   繁体   English

来自关联PHP数组的Javascript数组/对象顺序

[英]Javascript array/object order from associative PHP array

Before I describe the issue, please forgive any incorrect terms and accidental references to objects instead of arrays and vice-versa, I'm not completely up to speed on this but working my way through it. 在描述问题之前,请原谅任何不正确的术语和对象的偶然引用,而不是数组,反之亦然,我并没有完全加快速度,但是会尽力而为。

I have the following array in PHP saved as a session variable: 我在PHP中将以下数组另存为会话变量:

{"CategoryF":[],"CategoryA":["Life","There","People","Land","Family"],"CategoryC":["Various"]}

After a thumbnail in a grid of images is dragged into a new order, it execute a function in javascript and makes a call to a PHP script using ajax . 将图像网格中的缩略图拖动到新的顺序后,它将执行javascript的函数,并使用ajax调用PHP脚本。 It currently only retrieves the most up to date version of a session array. 当前,它仅检索会话阵列的最新版本。 It will later progress to make the necessary steps to save the updated array back to session variable and database: 稍后将进行必要的步骤以将更新后的数组保存回会话变量和数据库中:

var sorty = Sortable.create(thumbcontainer, { 
        animation: 250,
        draggable: "img",
        dataIdAttr: 'id',
        onUpdate: function (/**Event*/evt) {
            var orderList = sorty.toArray();
            var catsArray = 
            $.ajax({
                type: 'POST',
                url: 'includes/proc_cats.php',
                dataType: 'json'
            }).done(function(returnedCatsArray) {
                console.log(returnedCatsArray);
            });
            console.log('Dragged. Order is: ' + orderList);
        }
    });

proc_cats.php proc_cats.php

<?php
// Access the existing session
session_start();
// $catsArray is a session variable, in the format as above.
$catsArray = json_encode($_SESSION['categoriesPics']);
echo $catsArray;
?>

The var orderList will produce a string with the order of each thumbnail by id, separated by comma: '42,35,95,12,57' . var orderList将产生一个字符串,其中每个缩略图的顺序都由id组成,并以逗号分隔: '42,35,95,12,57'

The console shows the PHP array as a javascript array fine but in a different order. 控制台将PHP数组显示为javascript数组,但顺序不同。 I want to be able to insert the string containing the orders into the array and save it back into the database. 我希望能够将包含订单的字符串插入数组并将其保存回数据库中。 It will associate with its relevant category, similar to: 它将与相关类别相关联,类似于:

{"CategoryF":[],"CategoryA":["Life":["23,74,47,12,86,83,12"],"There","People","Land","Family"],"CategoryC":["Various"]}

But can't lose the order as other parts of the site reference the array by indices using array_keys . 但是不能失去顺序,因为站点的其他部分使用array_keys通过索引引用数组。 The console produces: 控制台产生:

Object:
   CategoryA:Array[0]
   CategoryC:Array[0]
   CategoryF:Array[5]

Have I missed something? 我错过了什么吗? I believe that the overall array is an object rather than an array because it didn't have any index whereas the subcategories did and they get presented as an array. 我相信整个数组是一个对象而不是数组,因为它没有任何索引,而子类别却有,因此它们以数组的形式呈现。 array_keys in PHP have made it straightforward enough to work around any indexing problems up until now on the PHP side in other areas of the site but I'm wondering if the solution for the javascript side is something as straightforward? 到目前为止, PHP array_keys已经足够简单,可以解决站点其他区域的PHP方面的所有索引问题,但是我想知道javascript方面的解决方案是否这么简单? The subcategories currently have indices only because I've yet to associate and orderList with them so I'm not trying not to backtrack and build an index for the array as it's going to get difficult (unless there's a simple way to do this that I've overlooked). 这些子类别目前仅具有索引,因为我尚未将其与orderList关联,因此我不会尝试不回溯并为该数组建立​​索引,因为这样做会变得很困难(除非有一种简单的方法可以做到这一点,已被忽略)。

(This is a more specific version of a question I asked an hour ago that I've now deleted for being too broad). (这是一个小时前我问的一个问题的更具体版本,由于过于广泛,我已将其删除)。

I believe you have a slight confusion based on the terms 'associative array' and 'array'. 我相信基于术语“关联数组”和“数组”,您会有一些困惑。 A php associative array corresponds to a javascript object. php关联数组对应于javascript对象。 returnedCatsArray should be accessed similar to $catsArray. 应该类似于$ catsArray来访问returnCatsArray。 ie. 即。 with keys. 与钥匙。 If one of those keys returns an an actual array, you can then index into it. 如果这些键之一返回一个实际数组,则可以对其进行索引。

php array_keys would be Object.keys(returnedCatsArray) in javascript. php array_keys将是JavaScript中的Object.keys(returnedCatsArray)。

From further research it appears this is just not doable. 从进一步的研究来看,这似乎是行不通的。 So the best way to do this may be to provide an order array alongside my category array. 因此,执行此操作的最佳方法可能是在我的类别数组旁边提供一个订单数组。

If I add the additional code of: 如果添加以下附加代码:

$parentCatOrder = array_keys($catsArray);

in my proc_cats.php script I have a concise way of generating an index reference for my original array on the fly each time. 在我的proc_cats.php脚本中,我有一种简洁的方式可以为运行中的原始数组每次生成索引引用。 This produces an array similar to: 这将产生类似于以下内容的数组:

$parentCatOrder = {'categoryF', 'categoryA', 'categoryC'};

which has an index that I can refer to that keeps its order. 我可以参考的索引保持其顺序。 So $parentCatOrder[2] will always produce 'categoryC' unless I've changed the array myself. 因此,除非我自己更改了数组,否则$parentCatOrder[2]将始终生成'categoryC'

I then return both arrays to javascript using the following: 然后,使用以下命令将两个数组都返回给javascript:

$return_data['catsarray'] = $catsArray;
$return_data['parentcatsorder'] = $parentCatOrder;
// Encode it back into a JSON object before sending
echo json_encode($return_data);

In javascript I can reference returnedCatsArray.catsarray[returnedCatsArray.parentcatsorder[1]][3] if I'm working with an index of 1-3 and guarantee this will produce the same result for every user unless the array has been changed by the user. 在javascript中,如果我使用1-3的索引,则可以引用returnedCatsArray.catsarray[returnedCatsArray.parentcatsorder[1]][3] ,并确保除非每个数组都被数组更改,否则这将为每个用户产生相同的结果。用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM