我在将多维数组[N] [N]从javascript发送到PHP时遇到了一些麻烦,我已经尝试了很多我在这里找到的解决方案,但我不知道我做错了什么。
我的JQuery代码(从HTML表中保存值):
$rowArray = {};
for ($i = 0; $i < $myRows.length; $i++) {
$row = $($myRows[$i]).find('td');
$rowArray[$i] = {};
for ($j = 0; $j < $row.length - 1; $j++) {
$rowArray[$i][$j] = $($row[$j]).html();
}
}
然后:
$myJsonString = JSON.stringify($rowArray);
$.ajax({
type: "POST",
url:"../../download/myStore.php",
data: { table: $myJsonString },
success: function(data){
console.log(data);
}
});
PHP方面:
echo $_POST['table']; //just to see what is coming, but i want to work as array
//$data = json_decode($_POST['table'],true); -> when I echo $data, the output is an error Array to String conversion
输出:
{"0":{"0":"Cadastrado em","1":"Data da Venda","2":"Empreendimento","3":"Bloco/<br>Unidade","4":"Cliente/<br>Parceiro","5":"Valor","6":"Filial","7":"Gerente","8":"Corretor","9":"Veículo"},"1":{"0":"27/04/2016","1":"11/04/2016","2":"Villa Flora Hortolândia - Cond. 06","3":"Bloco/Torre: 13, Unidade: 283","4":"Lidiane Sasaki Santana","5":"20.664.259","6":"Campinas","7":"NATAL","8":"WILLIAM PILOTO","9":"Internet"},"2":{"0":"12/04/2016","1":"12/04/2016","2":"Lifespace Curitiba","3":"Bloco/Torre: 1, Unidade: 2404","4":"ANA","5":"351.000","6":"Curitiba","7":"André Barbosa de Lima","8":"Daniele","9":"Google"},"3":{"0":"12/04/2016","1":"12/04/2016","2":"ROSSI ATUAL MORADA","3":"Bloco/Torre: 3, Unidade: 407","4":"BERNADETE STARKE","5":"245.000","6":"Curitiba","7":"André Barbosa de Lima","8":"Dranka","9":"Google"},"4":{"0":"12/04/2016","1":"12/04/2016","2":"Lifespace Curitiba","3":"Bloco/Torre: 2, Unidade: 1105","4":"FLAVIA AMARAL","5":"272.500","6":"Curitiba","7":"André Barbosa de Lima","8":"Jesus","9":"Yahoo"},"5":{"0":"12/04/2016","1":"12/04/2016","2":"Lifespace Curitiba","3":"Bloco/Torre: 2, Unidade: 1809","4":"itajana","5":"270.500","6":"Curitiba","7":"André Barbosa de Lima","8":"Daniele","9":"Site Rossi"},"6":{"0":"27/04/2016","1":"14/04/2016","2":"Villa Flora Hortolândia - Cond. 05","3":"Bloco/Torre: 5, Unidade: 41","4":"Andre Fernando Da Silva Gradino","5":"184.303","6":"Campinas","7":"NATAL","8":"TIAGO","9":"Cadastro Manual"}}
如何访问每个数组中的每个索引以获取我的值?
每当我尝试使用像$_POST['table'][0]
这样的索引时,输出就是'{'
对不起我的英语并对这个noob问题感到抱歉,但我已经在这里停留了好几个小时,并且已经在谷歌和谷歌上尝试了很多解决方案。
答案 0 :(得分:2)
正如你已经在你的代码里面(但已被注释掉)你可以使用json_decode($_POST['table'],true);
当你使用这个函数时,你得到一个数组,有7个(在这个例子中)。
像这样:
$data = json_decode($_POST['table'], true);
发生错误,因为您echo
变量。使用var_dump($data)
来查看它真的是一个数组。 $data[0]
,$data[1]
,...然后保存数据。
编辑:因为你有嵌套数组,你必须再次访问子数组,如下所示:
$data = json_decode($_POST['table'], true);
$var = $data[0][0]; // holds "Cadastrado em"
$var2 = $data[0][1]; // holds "Data da Venda"
我不确定,如果是这样的话,你在数组中有一个数组。
答案 1 :(得分:1)
使用json_decode将字符串文字转换为PHP对象。
$json = '["apple","orange","banana","strawberry"]';
$ar = json_decode($json);
// access first element of $ar array
echo $ar[0]; // apple
答案 2 :(得分:-1)
您的注释行
department_id
很好,但是你不能回显数组。这将失败:
def allocation_substitute(self,cr,uid,ids,roster_day,context=None):
sub_day=vals.get(roster_day)
sub_time_slot=vals.get(time_slot)
allocation_obj=self.pool.get('roster.allocation')
original_employee_id = allocation_obj.browse(cr,uid, values['emp_id']).id
original_employee_roster=allocation_obj.browse(cr,uid, original_employee_id).roster_type
emp_id = 1 # Search from hr.employee
department_id = 1 # Search from hr.department
values={'emp_id': emp_id,
'department_id': department_id,
}
allocation_id=allocation_obj.create(cr, uid, value, context=context)
return True
这将有效:
$data = json_decode($_POST['table'],true);