注意:尝试在 PHP 中增加关联数组时未定义索引
我试图在每次出现重复变量时增加 $variable 的值。我不确定这在语法上是否正确,但我认为这在语义上是正确的。 var_dump 似乎吐出正确的输出,但我收到此错误: 注意:未定义索引...
$newarray = array();
foreach ($array as $variable)
{
$newarray[$variable]++;
var_dump($newarray);
}
$array = (0 => h, 1 => e, 2 => l, 3=> l, 4=>o);
目标:
'h' => int 1
'e' => int 1
'l' => int 2
'o' => int 1
我的代码有效,只是我收到一些奇怪的通知。
I'm trying to increment the value for $variable each time a duplicate variable occurs. I'm not sure if this is syntactically correct, but I think this is semantically correct. var_dump seems to spit out the correct outputs, but i get this error: Notice: Undefined index...
$newarray = array();
foreach ($array as $variable)
{
$newarray[$variable]++;
var_dump($newarray);
}
$array = (0 => h, 1 => e, 2 => l, 3=> l, 4=> o);
goal:
'h' => int 1
'e' => int 1
'l' => int 2
'o' => int 1
My code works, it's just that I get some weird NOTICE.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
看一下函数 array_count_values()。它正是您想要做的。
来自 php.net 的示例:
结果:
Take a look at the function array_count_values(). It does exactly what you are trying to do.
Sample from php.net:
Result:
在现代 PHP 中,您可以通过利用空合并运算符来避免
isset()
调用。在下面的代码片段中,如果尚未声明变量,则该技术将变量设置为 0。然后你可以自由地使用一系列速记操作,例如连接、算术等。或者如果你想继续使用
++
,那么你可以使用“空合并赋值运算符”。如果变量已经声明,则赋值右侧的逻辑甚至不会执行。下面的代码片段将与上面的代码片段执行相同的操作。至于您的示例数据以及所需的字母和计数结果,我可能会使用完全本机的功能方法。 (演示)
对于相同的结果,吸引力要低得多的技术:
In modern PHP, you can avoid the
isset()
call by leveraging the null coalescing operator. In the snippet below, the technique sets the variable to 0 if the variable is not yet declared. Then you can freely use a range of shorthand manipulations such as concatenation, arithmetic, etc.Or if you want to continue using
++
, then you can use the "null coalescing assignment operator". The logic on the right side of assignment is not even executed if the variable is already declared. The below snippet will perform identically to the above snippet.As for your sample data and desired result of letters and counts, I'd probably use a fully native, functional approach. (Demo)
Much less attractive technique for the same result:
但您也可以使用 array_count_values() 代替。
But you could also use array_count_values() instead.
我真的很喜欢 mickmackusa 使用 null 合并
,但您可以使用数组元素引用保存第二次查找,如果不存在,该引用会自动插入
null
元素:幸运的是,增量运算符认为
null
code> 为零并根据需要递增到 1。I really like mickmackusa's use of null coalesce in
but you can save the second lookup using an array element reference which automatically inserts a
null
element if not present:Luckily, the increment operator considers
null
to be zero and increments to 1 as desired.这利用了引用接受未定义变量并且 PHP 静默创建它们的事实:
这只使用查找变量、键变量和值变量各一次,如果未定义则设置,如果定义则递增,所有这些都不会抛出警告(或最终抛出错误)在 PHP 9) 中,
异或运算符之所以有效,是因为 PHP 始终必须计算两边。
完成后请务必取消设置($_)。
This takes advantage of the fact that references accept undefined variables and PHP silently creates them:
This only uses the lookup, key, and value variables once each, and sets if undefined and increments if defined, all without throwing a warning (or eventually an error in PHP 9)
xor operator works because PHP always has to evaluate both sides.
Make sure to unset($_) when you're done.
您增加了错误的内容,请尝试以下操作:
You are incrementing the wrong thing, try this instead: