在php中合并具有相同id但不同值的数组元素

时间:2016-05-10 11:52:21

标签: php arrays

我有下面提到的数据形式: -

 Array
(
    [0] => stdclass Object
        (
            [legend_id] => 1
            [Xdata] => Array
                (
                    [0] => stdclass Object
                        (
                            [id] => 21-05-2016
                        )

                    [1] => stdclass Object
                        (
                            [id] => 22-05-2016
                        )

                    [2] => stdclass Object
                        (
                            [id] => 20-05-2016
                        )

                    [3] => stdclass Object
                        (
                            [id] => 23-05-2016
                        )

                )

            [Ydata] => Array
                (
                    [0] => stdclass Object
                        (
                            [val] => 10
                        )

                    [1] => stdclass Object
                        (
                            [val] => 20
                        )

                    [2] => stdclass Object
                        (
                            [val] => 49
                        )

                    [3] => stdclass Object
                        (
                            [val] => 29
                        )

                )

        )

    [1] => stdclass Object
        (
            [legend_id] => 2
            [Xdata] => Array
                (
                    [0] => stdclass Object
                        (
                            [id] => 21-05-2016
                        )

                    [1] => stdclass Object
                        (
                            [id] => 22-05-2016
                        )

                    [2] => stdclass Object
                        (
                            [id] => 23-05-2016
                        )

                )

            [Ydata] => Array
                (
                    [0] => stdclass Object
                        (
                            [val] => 50
                        )

                    [1] => stdclass Object
                        (
                            [val] => 70
                        )

                    [2] => stdclass Object
                        (
                            [val] => 80
                        )

                )

        )

    [2] => stdclass Object
        (
            [legend_id] => 3
            [Xdata] => Array
                (
                    [0] => stdclass Object
                        (
                            [id] => 21-05-2016
                        )

                    [1] => stdclass Object
                        (
                            [id] => 22-05-2016
                        )

                    [2] => stdclass Object
                        (
                            [id] => 23-05-2016
                        )

                )

            [Ydata] => Array
                (
                    [0] => stdclass Object
                        (
                            [val] => 40
                        )

                    [1] => stdclass Object
                        (
                            [val] => 60
                        )

                    [2] => stdclass Object
                        (
                            [val] => 30
                        )

                )

        )

    [3] => stdclass Object
        (
            [legend_id] => 4
            [Xdata] => Array
                (
                    [0] => stdclass Object
                        (
                            [id] => 21-05-2016
                        )

                    [1] => stdclass Object
                        (
                            [id] => 22-05-2016
                        )

                    [2] => stdclass Object
                        (
                            [id] => 23-05-2016
                        )

                )

            [Ydata] => Array
                (
                    [0] => stdclass Object
                        (
                            [val] => 40
                        )

                    [1] => stdclass Object
                        (
                            [val] => 60
                        )

                    [2] => stdclass Object
                        (
                            [val] => 30
                        )

                )

        )

)

我必须将其转换为下面提到的格式: -

foreach ($query->result() as $row) { 
    $out = array(); 
    foreach($row as $aData) { 
        $out['LegendID']=$aData['LegendID']; 
        $out['XData'][]=array('id'=>$aData['XData']); 
        $out['YData'][]=array('val'=>$aData['YData']); 
    } 
    print_r($out); 
}

这是我到目前为止所拥有的

[data-role="footer"] {
    left: 0;
    right: 0;
    width: 100%;
    position: fixed;
    z-index: 1000;
}

2 个答案:

答案 0 :(得分:0)

使用以下代码:

$result = array();
foreach ( $array as $key => $value ) {
    $result[$value['LegendID']]['LegendID'] = $value['LegendID'];
    $result[$value['LegendID']]['XData'][] = (object)array('id' =>$value['XData']);
    $result[$value['LegendID']]['YData'][] = (object)array('id' =>$value['YData']);
}
print_r((array_values($result)));

此处$array是您的$row

答案 1 :(得分:0)

我在newarray中得到了数组,之后我处理了它,在下面的提到过程中($ i = 0; $ iLegendID == $ lastlegend)                 {

                $legendId =$newarray[$i]->LegendID;
                $Xdata.='{"id":"'.$newarray[$i]->XData.'"},';
                $Ydata.='{"val":"'.$newarray[$i]->YData.'"},';                  

            }
            else{
                $lastlegend=$newarray[$i]->LegendID;

                $datastring .= ' {
                                "legend_id": "'.$legendId.'",
                                "Xdata":
                                    [
                                        '.rtrim($Xdata,',').'
                                    ],
                                "Ydata":
                                    [
                                        '.rtrim($Ydata,',').'
                                    ]
                            },';

                $Xdata ='{"id":"'.$newarray[$i]->XData.'"},';
                $Ydata ='{"val":"'.$newarray[$i]->YData.'"},';

            }

        }

        $datastring .= ' {
                        "legend_id": "'.$legendId.'",
                        "Xdata":
                            [
                                '.rtrim($Xdata,',').'
                            ],
                        "Ydata":
                            [
                                '.rtrim($Ydata,',').'
                            ]
                    }';

        $Xdata ='{"id":"'.$newarray[$i]->XData.'"},';
        $Ydata ='{"val":"'.$newarray[$i]->YData.'"},';

        $datastring = '['.$datastring.']';
        $jdata = json_decode($datastring);