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

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

问题描述

我是新在PHP中的数组,我试图总结我的周围如何做一个多维关联数组的头脑。我想数组看起来像这样,当我使用的print_r:

 阵列([0] =>数组([警示] =>警告[邮件] =>测试))

相反,我得到这样的:

 阵列([0] =>数组([警示] =>数组([邮件] =>测试)))

在code我使用的是这样的:

  $ alert_array =阵列();$ alert_array [] [警报] [电子邮件] =测试;

我想尝试这样的东西会的工作,但很明显,我的语法是有点过。我觉得我有点很正确的轨道上,但:?

  $ alert_array [] [[警报] [电子邮件] =测试;

您的帮助谢谢(对不起,如果这是超级基本的,我无法找到解决任何问题,这完全一致)!


解决方案

  $ alert_array =阵列();
$ alert_array [] =阵列('警报'=>'警报','邮件'=>'测试');
...
后续代码var_dump($ alert_array);

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

  $ alert_array [$关键] [警报] =警告;
$ alert_array [$关键] [电子邮件] =测试;

您不得不与计数器循环了。

如果你使用PHP 5.4+,你可以使用短阵语法:

  $ alert_array = [];
$ alert_array [] = ['警报'=> 警报,电子邮件= GT; '测试'];

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 ) )

Instead I get this:

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);

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.

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天全站免登陆