php 按共享子数组值对多维数组中的数据集进行分组,并将累积值推入组的列中

mccptt67  于 2023-11-16  发布在  PHP
关注(0)|答案(3)|浏览(101)

我正在使用PHP,我有以下数组,并一直试图弄清楚如何以以下方式将其展平。这是一个使用json_decode从JSON创建的数组,最终结果将是另一个JSON文件。
因此,无论我们有一个唯一的名称,patternTypetype在父数组的每个元素中总是相同的。我需要一种方法来基本上“折叠”数组和合并的数组,同时添加新的元素到动作数组。
当前数据结构。

[
    'abc' => [
        [
            'foo' => '*',
            'action' => 'Read',
            'resource' => [
                'name' => 'name_1',
                'patternType' => 'fixed',
                'type' => 'partition',
            ],
        ],
        [
            'foo' => '*',
            'action' => 'Write',
            'resource' => [
                'name' => 'name_1',
                'patternType' => 'fixed',
                'type' => 'partition',
            ],
        ],
        [
            'foo' => '*',
            'action' => 'Read',
            'resource' => [
                'name' => 'name_2',
                'patternType' => 'fixed',
                'type' => 'partition',
            ],
        ],
        [
            'foo' => '*',
            'action' => 'Alter',
            'resource' => [
                'name' => 'name_2',
                'patternType' => 'fixed',
                'type' => 'partition',
            ],
        ],
    ],
];

字符串
需要的数据结构

Array
(
    [abc] => Array
            [0] => Array
                (
                    [foo] => *
                    [action] => Array
                        (
                            [0] => Read
                            [1] => Write
                        )
                    [resource] => Array
                        (
                            [name] => name_1
                            [patternType] => fixed
                            [type] => partition
                        )

                )
            [1] => Array
                (
                    [foo] => *
                    [action] => Array
                        (
                            [0] => Read
                            [1] => Alter
                        )
                    [resource] => Array
                        (
                            [name] => name_2
                            [patternType] => fixed
                            [type] => partition
                        )

                )
)


我试过各种函数,比如array_merge,array_merge_recursive等等.我也试过遍历数组,创建一个二级数组,并在那里正确地创建数据结构,但我总是有问题,弄清楚如何引用父数组在这种情况下,b/c子数组的顺序总是不同的。

9cbw7uwe

9cbw7uwe1#

array_values()清除不需要的键可以通过在结果数组中推送引用变量来避免。这是一种更直接的方法。
我假设“资源名”是标识数据集唯一性的可靠方法。Declare通过这个标识符引用变量。
如果以前没有遇到过标识符,则将action值转换为一个单独元素数组,定义引用变量的数据,然后推入结果数组。如果以前遇到过标识符,则只需将新的action值推入引用的action子数组。
产品编号:(Demo

$result = [];
foreach ($array as $key => $sets) {
    foreach ($sets as $set) {
        $id = $set['resource']['name'];
        if (!isset($ref[$id])) {
            $set['action'] = (array) $set['action'];
            $ref[$id] = $set;
            $result[$key][] = &$ref[$id];
        } else {
            $ref[$id]['action'][] = $set['action'];
        }
    }
}
var_export($result);

字符串
或者,如果您只想改变输入数组而不生成新数组,则调整引用以指向第一个遇到的给定name值集合的action子数组。
代码:(Demo

foreach ($array as $key => &$sets) {
    foreach ($sets as $i => &$set) {
        $id = $set['resource']['name'];
        if (!isset($ref[$id])) {
            $set['action'] = (array) $set['action'];
            $ref[$id] = &$set['action'];
        } else {
            $ref[$id][] = $set['action'];
            unset($array[$key][$i]);
        }
    }
}
var_export($array);

7lrncoxx

7lrncoxx2#

要实现所需的数据结构,可以循环遍历原始数组,并使用临时关联数组按**“name”**字段对元素进行分组。然后可以从这个临时数组创建所需的结构。下面是一个PHP代码示例:

$outputArray = [];

foreach ($inputArray as $key => $items) {
    $tempArray = [];

    foreach ($items as $item) {
        $name = $item['resource']['name'];

        if (!isset($tempArray[$name])) {
            $tempArray[$name] = [
                'foo' => $item['foo'],
                'action' => [],
                'resource' => $item['resource'],
            ];
        }

        $tempArray[$name]['action'][] = $item['action'];
    }

    $outputArray[$key] = array_values($tempArray);
}
echo "<pre>";
print_r($outputArray);

字符串
这将在$outputArray中为您提供给予所需的数据结构。

deyfvvtc

deyfvvtc3#

这是给你的解决方案

<?php
$array = [
    "abc" => [
        [
                    "foo" => "*",
                    "action" => "Read",
                    "resource" => [
                            "name" => "name_1",
                            "patternType" => "fixed",
                            "type" => "partition"
                        ]

        ],
        [
                    "foo" => "*",
                    "action" => "Write",
                    "resource" => [
                            "name" => "name_1",
                            "patternType" => "fixed",
                            "type" => "partition"
                        ]

        ],
        [
                    "foo" => "*",
                    "action" => "Read",
                    "resource" => [
                            "name" => "name_2",
                            "patternType" => "fixed",
                            "type" => "partition"
                        ]

        ],
         [
                    "foo" => "*",
                    "action" => "Alter",
                    "resource" => [
                            "name" => "name_2",
                            "patternType" => "fixed",
                            "type" => "partition"
                        ]

        ],
    ]
    
];

$grouparr = [];
$tempVal;
$action;


foreach ($array['abc'] as $key => $val) {
   if (isset($tempVal['resource']['name']) && ($val['resource']['name'] == $tempVal['resource']['name'])){
    $grouparr[$key] = $val;
    $actions = [$action];
    array_push($actions, $grouparr[$key]['action']);
    $grouparr[$key]['action'] = $actions;

   } else {
     $action = $val['action'];
     $tempVal = $val;
   }
      
 }

print_r(array_values($grouparr));

?>

字符串

相关问题