简体   繁体   English

从jQuery发送多维数组到php

[英]Sending multidimensional array from jQuery to php

I am passing an array from jQuery to php. 我正在将数组从jQuery传递到php。

The array is generated from a table with this code: 该数组是使用以下代码从表生成的:

    var stitchChartArray = [];
    var row = 0;

    // turn stitch chart into array for php
    $('#stitchChart').find('tr').each(function (index, obj) {

        //first row is table head- "Block #"
        if(index != 0){
            stitchChartArray.push([]);
            var TDs = $(this).children();
            $.each(TDs, function (i, o) {
                var cellData = [$(this).css('background-color'), $(this).find("img").attr('src')];
                stitchChartArray[row].push(cellData);
            });
            row++;
        }

    });

In console it looks like this: 在控制台中,它看起来像这样:

[[["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(75, 75, 60)", "symbols/184.png"], ["rgb(75, 90, 60)", "symbols/177.png"], 7 more...], [["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(75, 75, 60)", "symbols/184.png"], ["rgb(75, 90, 60)", "symbols/177.png"], 7 more...], [["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(75, 75, 60)", "symbols/184.png"], 7 more...], [["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(98, 119, 57)", "symbols/210.png"], 7 more...], [["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(150, 150, 195)", "symbols/72.png"], 7 more...], [["rgb(75, 165, 105)", "symbols/187.png"], ["rgb(134, 158, 134)", "symbols/64.png"], ["rgb(165, 180, 180)", "symbols/171.png"], 7 more...], [["rgb(60, 150, 75)", "symbols/189.png"], ["rgb(120, 120, 90)", "symbols/225.png"], ["rgb(143, 163, 89)", "symbols/209.png"], 7 more...]]

It represents each row of a table->each cell of row->[0]rgb value of bg of cell [1]icon in cell. 它表示表格中的每一行->单元格中单元格[1] icon的bg的row-> [0] rgb值的每个单元格。

This jQuery code returns the correct element(and rgb value) from the array: 此jQuery代码从数组中返回正确的元素(和rgb值):

alert(stitchChartArray[1][1][0]); //row 1,cell 1, first value(rgb)

But when it gets sent to the php script with this: 但是,当它与此一起发送到php脚本时:

$.post('makeChartPackage.php', {'stitchChart[]': stitchChartArray }, function(data){
        alert(data);
    });

The php throws an error: 的PHP抛出一个错误:

Cannot use string offset as an array in /Users/tnt/Sites/cross_stitch/makeChartPackage.php on line 33 无法在第33行的/Users/tnt/Sites/cross_stitch/makeChartPackage.php中将字符串偏移量用作数组

$stitchChart = $_POST['stitchChart']; 
echo $stitchChart[1][1][0]; //line 33

I am assuming I am either constructing the array incorrectly or passing it to the php script incorrectly. 我假设我不是正确地构造了数组,或是不正确地将其传递给了php脚本。

EDIT: I did this to return the array to jQuery: 编辑:我这样做是为了将数组返回给jQuery:

$stitchChart = $_POST['stitchChart'];
print_r($stitchChart); 

And here was the result: Array ( [0] => rgb(75, 90, 60),symbols/177.png,rgb(75, 75, 60),symbols/184.png,rgb(75, 90, 60),symbols/177.png,rgb(98, 119, 57),symbols/210.png,rgb(180, 195, 105),symbols/388.png,rgb(165, 165, 120),symbols/235.png,rgb(75, 75, 60),symbols/184.png,rgb(90, 90, 45),symbols/195.png,rgb(120, 120, 75),symbols/156.png,rgb(105, 105, 105),symbols/163.png [1] => rgb(105, 105, 105),symbols/163.png,rgb(75, 75, 60),symbols/184.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 90, 60),symbols/177.png,rgb(165, 165, 120),symbols/235.png,rgb(120, 120, 75),symbols/156.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 90, 60),symbols/177.png,rgb(105, 105, 105),symbols/163.png,rgb(120, 120, 90),symbols/225.png [2] => rgb(105, 105, 105),symbols/163.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 75, 60),symbols/184.png,rgb(75, 90, 60),symbols/177.png,rgb(98, 119, 57),symbols/210.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 75, 60),symbols/184.png,rgb(105, 105, 105),symbols/163.png,rgb(120, 120, 90), 结果是:数组([0] => rgb(75,90,60),symbols / 177.png,rgb(75,75,60),symbols / 184.png,rgb(75,90,60 ),symbols / 177.png,rgb(98,119,57),symbols / 210.png,rgb(180,195,105),symbols / 388.png,rgb(165,165,120),symbols / 235 .png,rgb(75,75,60),symbols / 184.png,rgb(90,90,45),symbols / 195.png,rgb(120,120,75),symbols / 156.png,rgb( 105,105,105),symbols / 163.png [1] => rgb(105,105,105),symbols / 163.png,rgb(75,75,60),symbols / 184.png,rgb(75 ,90,60),symbols / 177.png,rgb(75,90,60),symbols / 177.png,rgb(165,165,120),symbols / 235.png,rgb(120,120,75) ,symbols / 156.png,rgb(75,90,60),symbols / 177.png,rgb(75,90,60),symbols / 177.png,rgb(105,105,105),symbols / 163。 png,rgb(120,120,90),symbols / 225.png [2] => rgb(105,105,105),symbols / 163.png,rgb(75,90,60),symbols / 177.png ,rgb(75,75,60),symbols / 184.png,rgb(75,90,60),symbols / 177.png,rgb(98,119,57),symbols / 210.png,rgb(75, 90,60),symbols / 177.png,rgb(75,75,60),symbols / 184.png,rgb(105,105,105),symbols / 163.png,rgb(120,120,90), symbols/225.png,rgb(105, 105, 105),symbols/163.png symbols / 225.png,rgb(105,105,105),symbols / 163.png

It appears the array is not multidimensional? 看来数组不是多维的吗?

$_POST['stitchChart'] in the context you have addressed it there is (effectively) a JSON representation of a multidimensional array, stored as a string. $_POST['stitchChart']在您已解决的上下文中,存在(有效)多维数组的JSON表示形式,存储为字符串。 When you treat a string as a multidimensional indexed array in PHP, you will get that error. 当您将字符串当作PHP中的多维索引数组时,您将得到该错误。 The first [x] is treated as a "string offset" - ie the character at position x - but the next and any subsequent [x] addresses can only be treated as arrays (you cannot get a substring of a single character) and will emit the error you have received. 第一个[x]被视为“字符串偏移量”(即,位置x处的字符),但是下一个和任何后续的[x]地址只能被视为数组(您无法获得单个字符的子字符串),并且发出您收到的错误。

To access your data as an array in PHP, you need to use json_decode() : 要在PHP中以数组形式访问数据,您需要使用json_decode()

$stitchChart = json_decode($_POST['stitchChart'],TRUE); 
echo $stitchChart[1][1][0];

EDIT 编辑

Because the jQuery data argument seemingly can't deal with multidimensional arrays, you should use Douglas Crockford's JSON-js library and pass the result into data as a string. 由于jQuery data参数似乎无法处理多维数组,因此您应该使用道格拉斯·克罗克福德(Douglas Crockford)的JSON-js库,并将结果作为字符串传递给data NB: use json2.js . 注意:使用json2.js

Here is how you could do this: 这是您可以执行的操作:

stitchChartArray = JSON.stringify(stitchChartArray);
$.post('makeChartPackage.php', {'stitchChart': stitchChartArray }, function(data){
    alert(data);
});

If you use this code, my original PHP suggestion should work as expected. 如果使用此代码,我最初的PHP建议应该可以按预期工作。

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

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