将未指定的数组值替换为 0
我想将除 work
和 home
之外的所有数组值替换为 0。
输入:
$array = ['work', 'homework', 'home', 'sky', 'door']
我的编码尝试:
$a = str_replace("work", "0", $array);
预期输出:
['work', 0, 'home', 0, 0]
另外,我的输入数据来自用户提交,并且数组元素的数量可能非常大。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
更优雅、更短的解决方案。
的&运算符是指向数组中特定原始字符串的指针。 (而不是该字符串的副本)
您可以通过这种方式为数组中的字符串分配一个新值。您唯一不能做的事情是任何可能扰乱数组顺序的事情,例如 unset() 或键操作。
上面示例的结果数组将是
A bit more elegant and shorter solution.
The & operator is a pointer to the particular original string in the array. (instead of a copy of that string)
You can that way assign a new value to the string in the array. The only thing you may not do is anything that may disturb the order in the array, like unset() or key manipulation.
The resulting array of the example above will be
循环将多次执行一系列操作。因此,对于数组中的每个元素,您将检查它是否等于您要更改的元素,如果是,请更改它。另外,请务必在字符串周围加上引号
以下是一些建议阅读:
http://www.php.net/manual/en/language.types.array.php。 php.net/manual/en/language.types.array.php
http:// /www.php.net/manual/en/language.control-structs.php
但是,如果您在循环等方面遇到困难,您可能需要阅读一些介绍性编程材料。这应该可以帮助您真正了解正在发生的事情。
A loop will perform a series of actions many times. So, for each element in your array, you would check if it is equal to the one you want to change and if it is, change it. Also be sure to put quote marks around your strings
Here is some suggested reading:
http://www.php.net/manual/en/language.types.array.php
http://www.php.net/manual/en/language.control-structures.php
But if you are struggling on stuff such as looping, you may want to read some introductory programming material. Which should help you really understand what's going on.
另一种更快的方法,但确实需要一个循环:
将输出:
A bit other and much quicker way, but true, need a loop:
Will output:
使用 array_map 的替代方案:
An alternative using array_map:
你可以尝试 array_walk 函数:
you may try array_walk function:
您还可以将数组作为 str_replace 上的参数 1 和 2...
You can also give array as a parameter 1 and 2 on str_replace...
只是 for 循环的一个小要点。许多人没有意识到第二个比较任务是在每次新的迭代中完成的。因此,如果是大数组或计算的情况,您可以通过执行以下操作来优化循环:
您可能还想查看 http://php.net/manual/en/function.array-replace.php 对于原始问题,除非代码确实是最终的:)
Just a small point to the for loop. Many dont realize the second comparing task is done every new iteration. So if it was a case of big array or calculation you could optimize loop a bit by doing:
You may also want to see http://php.net/manual/en/function.array-replace.php for original problem unless the code really is final :)
试试这个
Try This
本页上有一些技术可以实现零迭代函数调用——这在性能方面非常好。为了获得最佳的可维护性,我建议将目标字符串列表分隔为查找数组。通过引用修改原始数组值,您可以快速替换整个字符串并将非目标值 null 合并为 0。
代码:(演示)
输出:
您只需扩展
$keep
即可非常轻松、干净地扩展目标字符串列表。如果您不想要经典循环,则可以使用相同的技术而无需修改原始数组。 (演示)
There are a few techniques on this page that make zero iterated function calls -- which is good performance-wise. For best maintainability, I recommend separating your list of targeted string as a lookup array. By modifying the original array values by reference, you can swiftly replace whole strings and null coalesce non-targeted values to 0.
Code: (Demo)
Output:
You can very easily, cleanly extend your list of targeted strings by merely extending
$keep
.If you don't want a classic loop, you can use the same technique without modifying the original array. (Demo)
这是我的最终代码
this my final code