使用$ key的多数组分组是php中的对象

时间:2016-06-05 08:43:04

标签: php arrays group-by

我有一个数组,我希望按对象分组,键是一个对象。

$arr = array(
        array('cou_id' => "140", 'number' => "1", 'opera' =>array([0] => array("1", "2") [1] => array("2", "3"))),
        array('cou_id' => "141", 'number' => "1", 'opera' => array([0] => array("1", "2") [1] => array("5", "6"))),
        array('cou_id' => "140", 'number' => "4", 'opera' => array("3", "4")),
        array('cou_id' => "141", 'number' => "2", 'opera' =>array([0] => array("2", "3") [1] => array("6", "7")))   
    );

我想通过' cou_id'对此数组进行分组。并将数字推入array_number,使用array_merge()(PHP的功能)将opera合并到array_opera中。 结果将类似于:

   140 => array('cou_id' => "140", 'number' => "1", 'opera' =>array([0] => array("1", "2") [1] => array("2", "3") [2] => array("3", "4")))
   141 => array('cou_id' => "141", 'number' => "1", 'opera' => array([0] => array("1", "2") [1] => array("5", "6") [2] => array("2", "3") [3] => array("6", "7")))

请帮帮我!谢谢!!对不起,我的英语不好。

4 个答案:

答案 0 :(得分:1)

我认为你正在寻找这个:

$arr = array(
        array('cou_id' => "140", 'number' => "1", 'opera' => array("1", "2")),
        array('cou_id' => "141", 'number' => "1", 'opera' => array("1", "2")),
        array('cou_id' => "140", 'number' => "4", 'opera' => array("3", "4")),
        array('cou_id' => "141", 'number' => "2", 'opera' => array("2", "3"))   
    );

$out = array();
foreach($arr as $val){
    $out[$val['cou_id']]['array_number'][] = $val['number'];
    $out[$val['cou_id']]['array_opera'][] = $val['opera'];
}

echo '<pre>';
print_r($out);

更新:

$arr = array(
        array('cou_id' => "140", 'number' => "1", 'opera' =>array(array("1", "2"), array("2", "3"))),
        array('cou_id' => "141", 'number' => "1", 'opera' => array(array("1", "2"), array("5", "6"))),
        array('cou_id' => "140", 'number' => "4", 'opera' => array("3", "4")),
        array('cou_id' => "141", 'number' => "2", 'opera' =>array(array("2", "3"), array("6", "7")))   
    );

$out = array();
foreach($arr as $val){
    $out[$val['cou_id']]['array_number'][] = $val['number'];
    if(is_array($val['opera'][0])){
        foreach($val['opera'] as $newVal)
            $out[$val['cou_id']]['array_opera'][] = $newVal;
    }
    else
        $out[$val['cou_id']]['array_opera'][] = $val['opera'];
}

echo '<pre>';
print_r($out);

Working Example

答案 1 :(得分:1)

试试这个:

$array = array(
    array('cou_id' => "140", 'number' => "1", 'opera' => array("1", "2")),
    array('cou_id' => "141", 'number' => "1", 'opera' => array("1", "2")),
    array('cou_id' => "140", 'number' => "4", 'opera' => array("3", "4")),
    array('cou_id' => "141", 'number' => "2", 'opera' => array("2", "3"))   
);
$final_array = array();
foreach($array as $elem)
{
     if(!array_key_exists($elem['cou_id'],$final_array))
     {
         $final_array[$elem['cou_id']]['array_number'] = array();
         $final_array[$elem['cou_id']]['array_opera'] = array(); 
     }

     $final_array[$elem['cou_id']]['array_number'][] = $elem['number'];
     $final_array[$elem['cou_id']]['array_opera'][] = $elem['opera'];
}

答案 2 :(得分:0)

您的伪代码似乎是使用对象,而不是数组作为输入。这意味着潜在的方法将类似于以下内容。我选择了一个简单的类实现作为示例,因为您没有提供有关该类型的任何信息...

<?php
class something {
    public $cou_id;
    public $number;
    public $opera;
    public function __construct($cou_id, $number, $opera) {
        $this->cou_id = $cou_id;
        $this->number = $number;
        $this->opera = [$opera];
    }
}
$input = [
    0 => new something("140", "1", ["1", "2"]),
    1 => new something("141", "1", ["1", "2"]),
    2 => new something("140", "4", ["3", "4"]),
    4 => new something("141", "2", ["2", "3"]),
];

$output = [];
foreach ($input as $object) {
    $element = &$output[$object->cou_id];
    $element['array_number'][] = $object->number;
    $element['array_opera'][] = $object->opera;
}
print_r($output);

输出显然是:

Array
(
    [140] => Array
        (
            [array_number] => Array
                (
                    [0] => 1
                    [1] => 4
                )

            [array_opera] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 1
                                    [1] => 2
                                )

                        )

                    [1] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 3
                                    [1] => 4
                                )

                        )

                )

        )

    [141] => Array
        (
            [array_number] => Array
                (
                    [0] => 1
                    [1] => 2
                )

            [array_opera] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 1
                                    [1] => 2
                                )

                        )

                    [1] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 2
                                    [1] => 3
                                )

                        )

                )

        )

)

答案 3 :(得分:0)

请尝试以下代码:

$firstArray = array(
    array('cou_id' => "140", 'number' => "1", 'opera' =>array(array("1", "2"), array("2", "3"))),
    array('cou_id' => "141", 'number' => "1", 'opera' => array(array("1", "2"), array("5", "6"))),
    array('cou_id' => "140", 'number' => "4", 'opera' => array("3", "4")),
    array('cou_id' => "141", 'number' => "2", 'opera' =>array(array("2", "3"), array("6", "7")))   
);
$result = array();
foreach($firstArray as $e)
{
     if(!array_key_exists($e['cou_id'],$result)) {
         $result[$e['cou_id']]['array_number'] = array();
         $result[$e['cou_id']]['array_opera'] = array(); 
     }

     $result[$e['cou_id']]['array_number'][] = $e['number'];

     if (!is_array($e['opera'][0])) {
         $result[$e['cou_id']]['array_opera'][] = $e['opera'];
     } else {
         $result[$e['cou_id']]['array_opera'] = array_merge($result[$e['cou_id']]['array_opera'], $e['opera']);
     }
}
print_r($result);