多维关联数组 (PHP) [英] Multidimensional Associative Array (PHP)

查看:57
本文介绍了多维关联数组 (PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 PHP 数组的新手,我正在努力思考如何制作多维关联数组.当我使用 print_r 时,我希望数组看起来像这样:

I'm new to arrays in PHP and am trying to wrap my mind around how to make a multidimensional associative array. I'd like the array to look like this when I use print_r:

Array ( [0] => Array ( [alert] => alert [email] => Test ) )

相反,我明白了:

Array ( [0] => Array ( [alert] => Array ( [email] => Test ) ) )

我使用的代码是这样的:

The code I'm using is this:

$alert_array = array();

$alert_array[]["alert"]["email"] = "Test";

我认为尝试这样的事情会奏效,但显然我的语法有点不对.我觉得我有点走对了?:

I thought trying something like this would work, but obviously my syntax is a bit off. I think I'm somewhat on the right track though?:

$alert_array[][["alert"]["email"]] = "Test";

感谢您的帮助(抱歉,如果这太基础了,我找不到任何可以准确解决此问题的问题)!

Thank for your help (sorry if this is super basic, I couldn't find any questions that addressed this exactly)!

推荐答案

$alert_array = array();
$alert_array[] = array('alert' => 'alert', 'email' => 'Test');
...
var_dump($alert_array);

在您的情况下,您必须像这样指定 key:

In your case you'd have to specify key like so:

$alert_array[$key]["alert"] = "alert";
$alert_array[$key]["email"] = "Test";

你也必须有一个带计数器的循环.

You'd have to have a loop with counter too.

如果您使用的是 PHP 5.4+,则可以使用短数组语法:

If you're using PHP 5.4+ you could use short array syntax:

$alert_array = [];
$alert_array[] = ['alert' => 'alert', 'email' => 'Test'];

这篇关于多维关联数组 (PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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