array("msg1"=>"asdasd","msg2"=>"dasdsa"));/*----------如何将一个数据添加到数组中,使数组输出如下---------------------*//*  &"/> array("msg1"=>"asdasd","msg2"=>"dasdsa"));/*----------如何将一个数据添加到数组中,使数组输出如下---------------------*//*  &">
Home > Backend Development > PHP Tutorial > php 对多维数组的操作,该怎么解决

php 对多维数组的操作,该怎么解决

WBOY
Release: 2016-06-13 12:05:25
Original
1102 people have browsed it

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

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

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

输出结果是
Array ( [date] => Array ( [msg1] => asdasd [msg2] => dasdsa [0] => Array ( [msg3] => tel ) ) )
但我想要的是Array ( [date] => Array ( [msg1] => asdasd [msg2] => dasdsa [msg3] => tel ) )
请问如何才能做到?求大神帮忙。
急需!!!
------解决方案--------------------
<br />$a=array();<br />foreach($data as $k=>$v){<br />  $a['date']['msg'.$k]=$v;<br />}<br />print_r($a);<br />
Copy after login

------解决方案--------------------
$a=array("date"=>array("msg1"=>"asdasd","msg2"=>"dasdsa"));<br />$a['date']['msg3'] = 'tel';<br />print_r($a);
Copy after login
Array<br />(<br />    [date] => Array<br />        (<br />            [msg1] => asdasd<br />            [msg2] => dasdsa<br />            [msg3] => tel<br />        )<br /><br />)<br /><br />
Copy after login
Copy after login

------解决方案--------------------

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


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

------解决方案--------------------
$a=array("date"=>array("msg1"=>"asdasd","msg2"=>"dasdsa"));<br />$a = array_merge_recursive($a, array('date' => array('msg3'=>'tel')));<br />print_r($a);
Copy after login
Array<br />(<br />    [date] => Array<br />        (<br />            [msg1] => asdasd<br />            [msg2] => dasdsa<br />            [msg3] => tel<br />        )<br /><br />)<br /><br />
Copy after login
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