数组重组

时间:2016-07-17 12:04:53

标签: php arrays

我有以下查询结果:

Array
(
    [0] => stdClass Object
        (
            [TreatmentLog_ID] => 131
            [DateAdministered] => 2016-07-15
            [Notes] => 
            [Treatment_ID] => 144
            [AmountAdministered] => 1.5
            [Injectable_ID] => 2
            [InjectableName] => Baytril
        )

    [1] => stdClass Object
        (
            [TreatmentLog_ID] => 133
            [DateAdministered] => 2016-07-12
            [Notes] => 
            [Treatment_ID] => 146
            [AmountAdministered] => 1.2
            [Injectable_ID] => 20
            [InjectableName] => Vitamin C
        )

    [2] => stdClass Object
        (
            [TreatmentLog_ID] => 133
            [DateAdministered] => 2016-07-12
            [Notes] => 
            [Treatment_ID] => 147
            [AmountAdministered] => 1.3
            [Injectable_ID] => 21
            [InjectableName] => Vitamin E
        )
)

我希望能够将数组重组为以下内容:

Array
(
    [0] => stdClass Object
        (
            [TreatmentLog_ID] => 131
            [DateAdministered] => 2016-07-15
            [Notes] => 
            [Treatments] => Array
                (
                    [0] => stdClass Object
                        (
                            [Treatment_ID] => 144
                            [AmountAdministered] => 1.5
                            [Injectable_ID] => 2
                            [InjectableName] => Baytril
                        )

                )

        )

    [1] => stdClass Object
        (
            [TreatmentLog_ID] => 133
            [DateAdministered] => 2016-07-12
            [Notes] => 
            [Treatments] => Array
                (
                    [0] => stdClass Object
                        (
                            [Treatment_ID] => 146
                            [AmountAdministered] => 1.2
                            [Injectable_ID] => 20
                            [InjectableName] => Vitamin C
                        )

                    [1] => stdClass Object
                        (
                            [Treatment_ID] => 147
                            [AmountAdministered] => 1.3
                            [Injectable_ID] => 21
                            [InjectableName] => Vitamin E
                        )

                )

        )

)

注意如果InjectableName,第二个数组如何将AmountAdministeredInjectable_IDTreatment_IDarray合并到TreatmentLog_ID处理中是一场比赛。通常使用数组不是问题,但这个让我难过。我也无法更改查询。

我怎样才能在PHP中解决此问题?

2 个答案:

答案 0 :(得分:1)

使用issetarray_values函数的解决方案:

// $arr is your initial array
$result = [];
foreach ($arr as $obj) {
    $innerObj = (object)[ 'Treatment_ID' => $obj->Treatment_ID, 'AmountAdministered' => $obj->AmountAdministered,
                          'Injectable_ID' => $obj->Injectable_ID, 'InjectableName' => $obj->InjectableName ];

    if (!isset($result[$obj->TreatmentLog_ID])) {
        $result[$obj->TreatmentLog_ID] = (object)[
            'TreatmentLog_ID' => $obj->TreatmentLog_ID,
            'DateAdministered' => $obj->DateAdministered,
            'Notes' => $obj->Notes,
            'Treatments' => [$innerObj]
        ];
    } else {
        $result[$obj->TreatmentLog_ID]->Treatments[] = $innerObj;
    }
}

$result = array_values($result);
print_r($result);  // will output the expected result

答案 1 :(得分:0)

试试这个。

我们使用array_filter()$inputArray获取具有相同TreatmentLog_ID的所有元素。然后我们用array_map()转换那些过滤后的元素。我们必须使用clone创建每个元素的副本,因为它们的对象和对象是通过引用传递的。然后我们unset()我们在副本中不需要的密钥。

<?php

$inputArray = [
    0 => (object) [
        'TreatmentLog_ID' => 131,
        'DateAdministered' => '2016-07-15',
        'Notes' => '',
        'Treatment_ID' => 144,
        'AmountAdministered' => 1.5,
        'Injectable_ID' => 2,
        'InjectableName' => 'Baytril'
    ],

    1 => (object) [
        'TreatmentLog_ID' => 133,
        'DateAdministered' => '2016-07-12',
        'Notes' => '',
        'Treatment_ID' => 146,
        'AmountAdministered' => 1.2,
        'Injectable_ID' => 20,
        'InjectableName' => 'Vitamin C'
    ],

    2 => (object) [
        'TreatmentLog_ID' => 133,
        'DateAdministered' => '2016-07-12',
        'Notes' => '',
        'Treatment_ID' => 147,
        'AmountAdministered' => 1.3,
        'Injectable_ID' => 21,
        'InjectableName' => 'Vitamin E'
    ],
];

$transformedArray = [];
foreach ($inputArray as $key => $value)
{
    $transformedArray[$key] = [
        'TreatmentLog_ID' => $value->TreatmentLog_ID,
        'DateAdministered' => $value->DateAdministered,
        'Notes' => $value->Notes,
        'Treatments' => array_map(
            function ($v) {
                $copy = clone $v;

                unset($copy->Notes);
                unset($copy->DateAdministered);
                unset($copy->TreatmentLog_ID);

                return $copy;
            },
            array_filter($inputArray, function ($v) use ($value) {
                return $v->TreatmentLog_ID == $value->TreatmentLog_ID;
            })
        )
    ];
}

var_dump($transformedArray);

这给了我:

array(3) {
  [0]=>
  array(4) {
    ["TreatmentLog_ID"]=>
    int(131)
    ["DateAdministered"]=>
    string(10) "2016-07-15"
    ["Notes"]=>
    string(0) ""
    ["Treatments"]=>
    array(1) {
      [0]=>
      object(stdClass)#5 (4) {
        ["Treatment_ID"]=>
        int(144)
        ["AmountAdministered"]=>
        float(1.5)
        ["Injectable_ID"]=>
        int(2)
        ["InjectableName"]=>
        string(7) "Baytril"
      }
    }
  }
  [1]=>
  array(4) {
    ["TreatmentLog_ID"]=>
    int(133)
    ["DateAdministered"]=>
    string(10) "2016-07-12"
    ["Notes"]=>
    string(0) ""
    ["Treatments"]=>
    array(2) {
      [1]=>
      object(stdClass)#6 (4) {
        ["Treatment_ID"]=>
        int(146)
        ["AmountAdministered"]=>
        float(1.2)
        ["Injectable_ID"]=>
        int(20)
        ["InjectableName"]=>
        string(9) "Vitamin C"
      }
      [2]=>
      object(stdClass)#7 (4) {
        ["Treatment_ID"]=>
        int(147)
        ["AmountAdministered"]=>
        float(1.3)
        ["Injectable_ID"]=>
        int(21)
        ["InjectableName"]=>
        string(9) "Vitamin E"
      }
    }
  }
  [2]=>
  array(4) {
    ["TreatmentLog_ID"]=>
    int(133)
    ["DateAdministered"]=>
    string(10) "2016-07-12"
    ["Notes"]=>
    string(0) ""
    ["Treatments"]=>
    array(2) {
      [1]=>
      object(stdClass)#8 (4) {
        ["Treatment_ID"]=>
        int(146)
        ["AmountAdministered"]=>
        float(1.2)
        ["Injectable_ID"]=>
        int(20)
        ["InjectableName"]=>
        string(9) "Vitamin C"
      }
      [2]=>
      object(stdClass)#9 (4) {
        ["Treatment_ID"]=>
        int(147)
        ["AmountAdministered"]=>
        float(1.3)
        ["Injectable_ID"]=>
        int(21)
        ["InjectableName"]=>
        string(9) "Vitamin E"
      }
    }
  }
}

让我们分解我们构建Treatments的方式:

'Treatments' => array_map(
    function ($v) {
        $copy = clone $v;

        unset($copy->Notes);
        unset($copy->DateAdministered);
        unset($copy->TreatmentLog_ID);

        return $copy;
    },
    array_filter($inputArray, function ($v) use ($value) {
        return $v->TreatmentLog_ID == $value->TreatmentLog_ID;
    })
)