将数组转换为更小的数组
我一整天都在为这个问题伤透脑筋。 如何将以下数组转换
Array
(
[0] => Array
(
[compId] => 3081
[category] => Products
[rev] => 0.61
)
[1] => Array
(
[compId] => 3080
[category] => Plants
[rev] => 51
)
[2] => Array
(
[compId] => 3080
[category] => Products
[rev] => 6.1
)
)
为具有以下格式的数组:
Array(
'compId'=>array("3081","3080"),
'Products'=>array('0.61', '6.1'),
'Plants'=>array('0', '51')
);
前者由函数返回。请注意,后一个数组中的 0 不存在于前一个数组中。然而我确实需要保留关键值。我一直在尝试几个数组函数来使其工作,但我似乎无法解决问题。 任何帮助将非常感激。
让我尝试详细说明一下。后一个数组用作创建表的输入。 该表看起来像这样:
CompID | Products | Plants __________________________ 3081 | 0.61 | 0 __________________________ 3080 | 6.1 | 51
I've been breaking my head over this problem all day.
How can I transform the following array:
Array
(
[0] => Array
(
[compId] => 3081
[category] => Products
[rev] => 0.61
)
[1] => Array
(
[compId] => 3080
[category] => Plants
[rev] => 51
)
[2] => Array
(
[compId] => 3080
[category] => Products
[rev] => 6.1
)
)
Into an array with this format:
Array(
'compId'=>array("3081","3080"),
'Products'=>array('0.61', '6.1'),
'Plants'=>array('0', '51')
);
The former is being returned by a function. Please note that the 0 in the latter array isn't there in the former array. I do however need to preserve the key values. I've been trying several array functions to make it work but I just cant seem to solve the problem.
Any help would be very much appreciated.
Let me try elaborating a bit. The latter array is used as input to create a table.
The table would look like something as:
CompID | Products | Plants __________________________ 3081 | 0.61 | 0 __________________________ 3080 | 6.1 | 51
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解想要的结果:
If I understand the desired result:
这就是我理解所需结果的方式,问题从数组中删除了一项,所以我不完全确定。它会更改键,因此稍后可能需要一些键别名:
This is how I understand the desired result, the question drops one item from the array, so I'm not totally sure. And it changes the keys, so perhaps needs some key aliasing later on:
我很确定这可以进一步优化,尤其是通过针对不同的数组格式,但这应该可行。
I'm pretty sure this could be optimized further, not least by targeting a different array format, but this should work.