我可以在多维数组中存储数组吗? (PHP)

发布于 2024-10-31 10:00:27 字数 89 浏览 1 评论 0原文

是否可以将数组放入多维数组中?我有一个用户设置列表,我想在 JSON 数组中返回它,并且还有另一个数组存储在该 JSON 数组中...如果不可能,最好的方法是什么?

It is possible to put an array into a multi dim array? I have a list of user settings that I want to return in a JSON array and also have another array stored in that JSON array...what is the best way to do that if it isn't possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染年凉城似染瑾 2024-11-07 10:00:27

多维已经是数组内的数组。所以没有什么可以阻止你在那里放置另一个数组。有点像梦中之梦:P

如果您想赋予数组含义,只需使用关联数组

array(
   'SETTINGS' => array(
      'arr1' => array( 0, 1),
      'arr2' => array( 0, 1)  
   ),
   'DATA' => array(
      'arr1' => array( 0, 1),
      'arr2' => array( 0, 1)  
    )
)

编辑

要回答您的评论,$output_files[$file_id]['shared_with'] = $shared_info ; 翻译为(您的评论有一个额外的 ] 我删除了)

$shared_info = array(1, 2, 3);
$file_id     = 3;
$output_files = array(
  '3' => array(
     'shared_with' => array() //this is where $shared_info will get assigned
   )
);
//you don't actually have to declare it an empty array. I just did it to demonstrate.
$output_files[$file_id]['shared_with'] = $shared_info; // now that empty array is replaced. 

A multi dimension is already an array inside an array. So there's nothing stopping you from putting another array in there. Sort of like dreams within dreams :P

Just use associative arrays if you want to give your array meaning

array(
   'SETTINGS' => array(
      'arr1' => array( 0, 1),
      'arr2' => array( 0, 1)  
   ),
   'DATA' => array(
      'arr1' => array( 0, 1),
      'arr2' => array( 0, 1)  
    )
)

EDIT

To answer your comment, $output_files[$file_id]['shared_with'] = $shared_info; translates to (your comment had an extra ] which I removed)

$shared_info = array(1, 2, 3);
$file_id     = 3;
$output_files = array(
  '3' => array(
     'shared_with' => array() //this is where $shared_info will get assigned
   )
);
//you don't actually have to declare it an empty array. I just did it to demonstrate.
$output_files[$file_id]['shared_with'] = $shared_info; // now that empty array is replaced. 
聆听风音 2024-11-07 10:00:27

任何数组键都可以在 php 和 json 中拥有数组值。

php:

'key' => array(...)

json:

"key" : [...]

注意:php 不支持 C 或 C++ 中的多维数组。它只是一个包含另一个数组的数组元素。

any array key can have an array value in php, as well as in json.

php:

'key' => array(...)

json:

"key" : [...]

note: php doesn't support multidimensional arrays as in C or C++. it's just an array element containing another array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文