如何在 Php 中将相似键的数值数组值相乘?
$a = array ('x' => 2, 'y' => 3);
$b = array ( 'y' => 2, 'z' => 3);
// $c = $a * $b;
// i would like to have
// $c = array ('x' => 0, 'y' => 6, 'z' => 0);
$a = array ('x' => 2, 'y' => 3);
$b = array ( 'y' => 2, 'z' => 3);
// $c = $a * $b;
// i would like to have
// $c = array ('x' => 0, 'y' => 6, 'z' => 0);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想将任何相似的键相乘,您将需要获取键的列表。
array_keys 似乎就是这个功能。
这(希望)适用于您提交的任何一组键,而不仅仅是 x、y 和 z。
If you want to multiply any similar keys together, you will need to get a list of the keys.
array_keys would appear to be just the function for that.
This will (hopefully) work for any set of keys you hand in, not just x, y, and z.
您可以使用 array_map 并滥用 bcmul 来达到此目的:
You could use array_map and abuse bcmul for this purpose:
您必须使用库或自己定义叉积:
http://en.wikipedia.org/ wiki/Cross_product
在进一步检查你正在做的事情之后,看起来你似乎想要类似的东西:
同样,你必须创建自己的函数,并且你应该做一些错误检查值是否未定义。
You'll have to use a library or define the cross product yourself:
http://en.wikipedia.org/wiki/Cross_product
After further examination of what you're doing, it looks as though you'd like something along the lines of:
again, you'll have to make your own function, and you ought to do some error checking in case a value is undefined.