将数组转换为更小的数组

发布于 2024-11-19 18:19:29 字数 850 浏览 2 评论 0原文

我一整天都在为这个问题伤透脑筋。 如何将以下数组转换

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清晰传感 2024-11-26 18:19:29

如果我理解想要的结果:

$result = array();

foreach ( $array as $item ) {
    $result['compId'][] = $item['compId'];
    $result[$item['category']][] = $item['rev'];
}

print_r($result);

If I understand the desired result:

$result = array();

foreach ( $array as $item ) {
    $result['compId'][] = $item['compId'];
    $result[$item['category']][] = $item['rev'];
}

print_r($result);
尹雨沫 2024-11-26 18:19:29

这就是我理解所需结果的方式,问题从数组中删除了一项,所以我不完全确定。它会更改键,因此稍后可能需要一些键别名:

foreach ( $array as $item )
    foreach( $item as $key => $value)
        $result[$key][] = $value
;

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:

foreach ( $array as $item )
    foreach( $item as $key => $value)
        $result[$key][] = $value
;
梦回旧景 2024-11-26 18:19:29
$categories = array_unique(array_map(function ($elem) { return $elem['category']; }, $array));
$tableRows = array('compId' => array_values(array_unique(array_map(function ($elem) { return $elem['compId']; }, $array))));

foreach ($tableRows['compId'] as $i => $compId) {
    foreach ($categories as $category) {
        $tableRows[$category][$i] = 0;
        foreach ($array as $elem) {
            if ($elem['category'] == $category && $elem['compId'] == $compId) {
                $tableRows[$category][$i] = $elem['rev'];
                break;
            }
        }
    }
}

我很确定这可以进一步优化,尤其是通过针对不同的数组格式,但这应该可行。

$categories = array_unique(array_map(function ($elem) { return $elem['category']; }, $array));
$tableRows = array('compId' => array_values(array_unique(array_map(function ($elem) { return $elem['compId']; }, $array))));

foreach ($tableRows['compId'] as $i => $compId) {
    foreach ($categories as $category) {
        $tableRows[$category][$i] = 0;
        foreach ($array as $elem) {
            if ($elem['category'] == $category && $elem['compId'] == $compId) {
                $tableRows[$category][$i] = $elem['rev'];
                break;
            }
        }
    }
}

I'm pretty sure this could be optimized further, not least by targeting a different array format, but this should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文