Home > Backend Development > PHP Tutorial > php 对多维数组的操作

php 对多维数组的操作

WBOY
Release: 2016-06-23 13:50:45
Original
1313 people have browsed it

我想将数据循环插入多维数组,请问该怎么做?
代码如下:

$a=array("date"=>array("msg1"=>"asdasd","msg2"=>"dasdsa"));/*----------如何将一个数据添加到数组中,使数组输出如下---------------------*//*   Array ( [date] => Array ( [msg1] => asdasd [msg2] => dasdsa[msg3] => dasdsa[msg4] => dasdsa ) )   */
Copy after login

我用了array_push函数,可是结果不是我想要的。
代码如下:
$a=array("date"=>array("msg1"=>"asdasd","msg2"=>"dasdsa"));array_push($a['date'],array('msg3'=>'tel'));print_r($a);
Copy after login

输出结果是
Array ( [date] => Array ( [msg1] => asdasd [msg2] => dasdsa [0] => Array ( [msg3] => tel ) ) )
但我想要的是 Array ( [date] => Array ( [msg1] => asdasd [msg2] => dasdsa [msg3] => tel ) )
请问如何才能做到?求大神帮忙。
急需!!!


回复讨论(解决方案)

$a=array();foreach($data as $k=>$v){  $a['date']['msg'.$k]=$v;}print_r($a);
Copy after login

$a=array("date"=>array("msg1"=>"asdasd","msg2"=>"dasdsa"));$a['date']['msg3'] = 'tel';print_r($a);
Copy after login
Copy after login
Array(    [date] => Array        (            [msg1] => asdasd            [msg2] => dasdsa            [msg3] => tel        ))
Copy after login
Copy after login
Copy after login

$b = array('msg3'=>'tel');foreach($b as $k=>$v){    $a['date'][$k] = $v;}print_r($a);
Copy after login

$a=array("date"=>array("msg1"=>"asdasd","msg2"=>"dasdsa"));$a['date']['msg3'] = 'tel';print_r($a);
Copy after login
Copy after login


Array(    [date] => Array        (            [msg1] => asdasd            [msg2] => dasdsa            [msg3] => tel        ) )
Copy after login

$a=array("date"=>array("msg1"=>"asdasd","msg2"=>"dasdsa"));$a['date'] = array_merge($a['date'],array('msg3'=>'tel'));print_r($a);
Copy after login
Array(    [date] => Array        (            [msg1] => asdasd            [msg2] => dasdsa            [msg3] => tel        ))
Copy after login
Copy after login
Copy after login

$a=array("date"=>array("msg1"=>"asdasd","msg2"=>"dasdsa"));$a = array_merge_recursive($a, array('date' => array('msg3'=>'tel')));print_r($a);
Copy after login
Array(    [date] => Array        (            [msg1] => asdasd            [msg2] => dasdsa            [msg3] => tel        ))
Copy after login
Copy after login
Copy after login

感谢大家帮忙!!
我将数组变为json

Array (           [date] => Array (                                      [msg1] => asdasd                                       [msg2] => dasdsa                                       [msg3] => tel                                      [num] => 3                                      )           ){      "date":                {                  "msg1":"asdasd",                  "msg2":"dasdsa",                  "msg3":"tel",                  "num":"3"                }}
Copy after login

但是在js里如何取出json的数据??单个取出,比如说我想取出num。

alert(json.date.num)

你有点本末倒置了
一般的说,客户端的灵活性要比服务端的差
所以应先决定客户端怎么做、需要什么样的数据后,再由服务端组装

<script type="text/javascript">	var json = {"date":{"msg1":"asdasd","msg2":"dasdsa","msg3":"tel","num":"3"}};	alert(json.date.num);</script>
Copy after login

var obj = JSON.parse(s); //s为你的JSON串
alert(obj.date.num);

多谢了。
写的时候不太了解php数组和json,由于是依靠json_encode()函数,也不太清楚json结构会是什么样。
但是你给的还不是我想要的结果,还有一些问题。
json:

date后面还有日期,日期是一个变量。日期可以做到循环变化,不用管。
msg后面也有一个变量。

//我想这样写,但是不管用//以下代码不管用,但是我想取出每个日期的num,再取出msg。day="这里有变化的日期";var i=json["date"+day]["num"];  //这样无输出var i=json["date"+day].num;  //这样也无输出if (i!=0){  for (var j=1;j<=i;j++)    {     alert(json["date"+day]["msg"+j]);   //输出信息。这个也不行    }}
Copy after login

我想让输出结果为这样:(日期1:“msg1的内容”、“msg2的内容”…………),(日期2:“msg1的内容”、“msg2的内容”…………)  …………不用管输出格式,只要得到每个日期的信息内容就行了。
Copy after login

请问该怎么写??

对了,得到的json已经转换了。

$.ajax({……………………………… success:function (data)		 {			 var c=data;			 var ss;		 	 json=eval("("+c+")");                 }});
Copy after login

多谢大家,我的问题已解决。结贴。。。


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template