热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

通过ajax到PHP的多维数组-MultidimensionalArraysviaajaxtoPHP

Okseriouslystrugglinghere.IamhavingsomeproblemstryingtosendamultdimensionalarraytoPH

Ok seriously struggling here. I am having some problems trying to send a multdimensional array to PHP via ajax. Here's what I have been trying:

好认真在这里挣扎。我试图通过ajax向PHP发送多维数组时遇到一些问题。这是我一直在尝试的:

To simplify rather than copy paste a wall of code:

为了简化而不是复制粘贴代码墙:

    peoplearray[0]  =  [name] => 'john'
                       [age]  => '28'
                       [sex]  => 'Male'
    peoplearray[1]  =  [name] => 'julie'
                       [age]  => '20'
                       [sex]  => 'Female'

    main_array['item'] = 'x';
    main_array['something'] = 'x';
    main_array['another'] = 'x';

I want to get this to php via post. I figured I may aswell just join them together as I am multidimensional anyway thus :

我希望通过帖子将其发送到php。我想我也可以将它们加在一起,因为我是多维的,因此:

    main_array['peoplearray'] = peoplearray;

now to do the ajax:

现在要做ajax:

// var data = JSON.stringify(main_array);

var send = $.ajax({
        type: "POST",
        cache: false,
        url: "theurl",
        data: {data:main_array} //I do change this `main_array` when using the above stringify!
});


send.done(function(msg) {
console.log(msg);
})

in PHP I am just doing the following right now:

在PHP中,我现在正在执行以下操作:

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

in firebug: (an empty string)

在萤火虫:(一个空字符串)

when I have the var data = JSON.stringify(main_array); uncommented I get the following: [][

当我有var data = JSON.stringify(main_array);取消注释我得到以下内容:[] [

if i add $data = json_decode($_POST['data']); to the php I get:

如果我添加$ data = json_decode($ _ POST ['data']);我得到的PHP:

Array ( )

Basically the main_array I realise does not need to be an array and so I can get that stuff across no problem but what I need to do is get the peoplearray over so that I can do some foreach etc... with it in php. Any help would be much appreciated I am sure I am just being stupid!

基本上我认识的main_array不需要是一个数组,所以我可以得到那些东西没有问题,但我需要做的是让人们发布,以便我可以做一些foreach等...用它在PHP中。任何帮助将不胜感激我相信我只是愚蠢!

EDIT: The reasoning behind this is that peoplearray could have 0 or 100 entries so I just need to get it to php so I can foreach it to do the DB inputs. If there is a better approach I would be very grateful to hear it as I am still pretty new to this.

编辑:这背后的原因是peoplearray可能有0或100个条目,所以我只需要将它转换为php,这样我就可以预先做它来进行数据库输入。如果有一个更好的方法,我会非常感激听到它,因为我仍然是新手。

EDIT: Thanks to Nicola's answer everything is passing fine except the important part which is mainarry.peoplearray - it is not appearing in the the return console.log and I cant access it in PHP. Any solutions on this or do I have to put the foreach intelligence in the Javascript and just send everything individually?

编辑:感谢Nicola的回答一切正常,除了mainarry.peoplearray的重要部分 - 它没有出现在return console.log中,我无法在PHP中访问它。任何解决方案或我是否必须将foreach智能放在Javascript中并单独发送所有内容?

2 个解决方案

#1


7  

First of all main_array is not an array but an object because in Javascript there are no associative arrays and for this reason

首先,main_array不是一个数组而是一个对象,因为在Javascript中没有关联数组,因此

main_array['peoplearray'] = peoplearray;

is equivalent to

相当于

main_array.peoplearray = peoplearray;

and you should declare main_array like this

你应该像这样声明main_array

var main_array = {};

then try to change your function like this:

然后尝试改变你的功能:

var send = $.ajax({
        type: "POST",
        dataType: "json",
        cache: false,
        url: "theurl",
        data: {data:main_array} 
});

and server side

和服务器端

header('Content-type: application/json');
$data= $_POST['data'];
echo json_encode($data);

#2


1  

I got it to work by keeping the peoplearray seperate.

我通过保持人员分离来实现它。

So I did as Nicola said and created mainarray as an object ie. declaring with curlies: {}

所以我按照尼古拉的说法做了,并创建了主阵列作为对象,即。用curlies声明:{}

The peoplearray I left as an array ie declaring with [], however then name,age&sex fields I created as an object ie. {} and then .push() them into the the peoplearray.

我作为一个数组离开的人员阵列,即用[]声明,然而我创建的名称,年龄和性别字段作为对象即。 {}然后.push()他们进入了人员阵列。

Then the ajax looked as follows:

然后ajax看起来如下:

var send = $.ajax({
        type: "POST",
        dataType: "json",
        cache: false,
        url: "theurl",
        data: {data:main_array, people:peoplearray} 
});

then with the PHP everything is available in the $_POST, and if you

然后使用PHP,$ _POST中有一切可用,如果你

echo json_encode($people); //or whatever var name it is stored as in the php 

the objects ie name,age,sex properties are shown in the

对象即名称,年龄,性别属性显示在

send.done(function(msg) {
console.log(msg);
})

推荐阅读
  • 如何在php中将mysql查询结果赋值给变量
    本文介绍了在php中将mysql查询结果赋值给变量的方法,包括从mysql表中查询count(学号)并赋值给一个变量,以及如何将sql中查询单条结果赋值给php页面的一个变量。同时还讨论了php调用mysql查询结果到变量的方法,并提供了示例代码。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • Python如何调用类里面的方法
    本文介绍了在Python中调用同一个类中的方法需要加上self参数,并且规范写法要求每个函数的第一个参数都为self。同时还介绍了如何调用另一个类中的方法。详细内容请阅读剩余部分。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • HashMap的相关问题及其底层数据结构和操作流程
    本文介绍了关于HashMap的相关问题,包括其底层数据结构、JDK1.7和JDK1.8的差异、红黑树的使用、扩容和树化的条件、退化为链表的情况、索引的计算方法、hashcode和hash()方法的作用、数组容量的选择、Put方法的流程以及并发问题下的操作。文章还提到了扩容死链和数据错乱的问题,并探讨了key的设计要求。对于对Java面试中的HashMap问题感兴趣的读者,本文将为您提供一些有用的技术和经验。 ... [详细]
  • 深入理解Java虚拟机的并发编程与性能优化
    本文主要介绍了Java内存模型与线程的相关概念,探讨了并发编程在服务端应用中的重要性。同时,介绍了Java语言和虚拟机提供的工具,帮助开发人员处理并发方面的问题,提高程序的并发能力和性能优化。文章指出,充分利用计算机处理器的能力和协调线程之间的并发操作是提高服务端程序性能的关键。 ... [详细]
  • 从批量eml文件中提取附件的Python代码实现方法
    本文介绍了使用Python代码从批量eml文件中提取附件的实现方法,包括获取eml附件信息、递归文件夹下所有文件、创建目的文件夹等步骤。通过该方法可以方便地提取eml文件中的附件,并保存到指定的文件夹中。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 31.项目部署
    目录1一些概念1.1项目部署1.2WSGI1.3uWSGI1.4Nginx2安装环境与迁移项目2.1项目内容2.2项目配置2.2.1DEBUG2.2.2STAT ... [详细]
author-avatar
手机用户2502876273
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有