我目前正在清理一些讨厌的代码。这段代码为一个现有数组定义了几个深度相当的新键:
$form['long_key_name_right_here_you_see']['title'] = 'Hello';
$form['long_key_name_right_here_you_see']['body'] = 'This be a greeting';
$form['long_key_name_right_here_you_see']['etc'] = 'This does nothing';
$form['another_long_key_name_right']['title'] = 'Hello!';
$form['another_long_key_name_right']['body'] = 'This be a greeting!';
$form['another_long_key_name_right']['etc'] = 'This does nothing.';
我更喜欢这样定义:
$arr_form = array('long_key_name_right_here_you_see' => array('title' => 'Hello',
'body' => 'This be a greeting',
'etc' => 'This does nothing'
),
'another_long_key_name_right' => array('title' => 'Hello!',
'body' => 'This be a greeting!',
'etc' => 'This does nothing.'));
$form = ($arr_form + $form);
- 我可以总是指望我的首选方法做相同的结果作为现有的情况?
- 我喜欢的方法会被认为是不清晰和毫无意义的吗?
本质上,我只是觉得在行中定义的一大堆相同的键在代码中看起来有点难看,但也许这只是我的想法,这是挑剔的。
相关PHP文档
+操作符返回右边的数组追加到左边数组;对于同时存在于两个数组中的键数组中匹配的元素将被使用右数组将被忽略。
注意:
接受的答案在我的情况下帮助了我。这个问题不一定有一个正确的答案,但可能会激励其他人,就像它对我一样。
由于我们使用的是单个数组单元格,因此我可能会使用指向长键的短指针来提高可读性。就我个人而言,我不喜欢过度使用array()函数调用,因为它有点难以阅读。
指针不会像临时变量那样占用额外的内存空间。
:
$form['long_key_name_right_here_you_see']['title'] = 'Hello';
$form['long_key_name_right_here_you_see']['body'] = 'This be a greeting';
$form['long_key_name_right_here_you_see']['etc'] = 'This does nothing';
会变成这样:
$target_form =& $form['long_key_name_right_here_you_see'];
$target_form = array(); // In case it's undefined. Drop this if it's not empty.
$target_form['title'] = 'Hello';
$target_form['body'] = 'This be a greeting';
$target_form['etc'] = 'This does nothing';
unset($target_form);
注意,unset语句将取消指针引用,而不是数组的内容。只有当您决定以后重用$target_form
时,才需要避免覆盖您的数据。
这可能比简单的注释更值得注意:小心指针。如果不小心使用,很容易覆盖数据,但很难调试。
$arr_form += $form;
不会给您想要的输出。您应该将变量反转,例如:
$form = array();
$arr_form = array(
'title' => 'Hello',
'body' => 'This be a greeting',
'etc' => 'This does nothing'
);
$form['long_key_name_right_here_you_see'] = $arr_form;
var_dump($form);
每个人都有不同的品味,语言(PHP, Python等)通常允许您做同样的事情以各种方式,满足各种品味和风格规范……
with
语句思路
@MaximKumpan的回答像是使用了Python和其他语言的with..as
语句。想象一下是这样的,
//if not exist, $form['long_key_name_right_here_you_see']=array();
with $form['long_key_name_right_here_you_see'] as $f {
$f['title'] = 'Hello';
$f['body'] = 'This be a greeting';
$f['etc'] = 'This does nothing';
}
注意:想象一下(!),真正的PHP代码是$f =& $form['...']
,以unset($f)
结尾,作为Max的代码。
一般性配置问题/解决方案
你的问题让我想起了我们之前在各种配置脚本中看到的一些东西。这里我们有一个典型的Mediawiki示例,为两种口味提供了两种样式:
$w = array( 'png', 'jpg', 'jp2', 'webp', '...'); // original
$w[] = 'pdf'; // "add one by one item" taste
$w[] = '...';
$new = array( 'pdf', 'ppt', 'jp2' );
$w = array_merge( $w, $new ); // "add all itens" taste
你还记得另一种方式
$w = $w + $new;
// CAUTION: array_merge, "+" and "+=" are not exactly the same thing!
但是,你的问题不是合并普通数组,它是关于"合并关联数组"…你也能做得很好,明白吗这是另一个类似的问题/答案(参见array_merge_recursive function)。
对于您的示例,array_merge
也是一个有效且可读的解决方案(在您的代码中替换$arr_form + $form
):
$form = array_merge($form, $arr_form); // not the same as (arr_form,$form)!
Mediawiki(上面的链接)是"最佳实践"的一个例子,并且(我也)更喜欢array_merge
函数而不是数组联合运算符("+")。