用PHP将项目添加到数组中的数组? [英] Adding items to an array within an array with php?

查看:36
本文介绍了用PHP将项目添加到数组中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,该数组包含一个包含更多数据的数组,这些数据存储到原始数组的键之一中.

I have an array that has an array containing more data stored into one of the keys in the original array.

例如:

$array2 = array('some_data');
$array1 = array(
'username' => $username,
'password' => $password,
'data' => $array2);

array1也可以多次出现,另一个单词为一个以上的用户存储数据.如何在$ array1的数据数组中添加更多数据"?

The array1 can also have multiple occurrences, another words store data for more then one user. How can I add more "data" to the data array within $array1?

我尝试做

$array1[0]['data'][] = array('diff_data');

但是我不断收到错误消息.有一个更好的方法吗?谢谢.

but I keep getting errors. Is there a better way to do this? Thank you.

推荐答案

您可以将新数据添加到

$array1['data'][] = 'new data';

,您可以像这样访问此数据:

and you can acces this data like this:

echo ($array1['data'][0]);

其中0是array2元素的索引.

Where 0 is the index of the array2 element.

这篇关于用PHP将项目添加到数组中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆