根据匹配值合并对象数组中的值

时间:2019-07-18 15:54:41

标签: php

我目前有一个采用这种格式的数组:

>>> rolling_window( df[u'Close'], 100 )
array([[ 43.9375    ,  44.25      ,  44.34375   , ...,  44.5       ,  44.59375   ,  44.625     ],
       [ 44.25      ,  44.34375   ,  44.8125    , ...,  44.59375   ,  44.625     ,  44.21875   ],
       [ 44.34375   ,  44.8125    ,  45.        , ...,  44.625     ,  44.21875   ,  44.8125    ],
       ...,
       [279.14001465, 279.51998901, 279.32000732, ..., 300.6499939 , 300.75      , 299.77999878],
       [279.51998901, 279.32000732, 279.20001221, ..., 300.75      , 299.77999878, 297.73999023],
       [279.32000732, 279.20001221, 278.67999268, ..., 299.77999878, 297.73999023, 297.42999268]])

我希望遍历数组,因此其结构如下所示:

for                         aRowINDEX in range( 1, 200 ):
    df[u'HurstEXP_COLUMN'][-aRowINDEX] = HurstEXP( df[u'Close'][:-aRowINDEX] )
    print( "[{0:>4d}]: DIFF( hurst() - HurstEXP() ) == {1:}".format( aRowINDEX,
                           ( hurst(    df[u'Close'][:-aRowINDEX] )
                           - HurstEXP( df[u'Close'][:-aRowINDEX] )
                             )
            )

如果“名称”匹配,我希望它合并“值”。

4 个答案:

答案 0 :(得分:1)

我已经对您的数据进行了一些更改以获取输出,这可能会解决您的问题。

<?php
$val = json_decode('[
{
    "name": "Size",
    "values": [
        "Small"
    ]
},
{
    "name": "Colour",
    "values": [
        "Red"
    ]
},
{
    "name": "Size",
    "values": [
        "Large"
    ]
},
{
    "name": "Colour",
    "values": [
        "Green"
    ]
}
]');
$output =[];
foreach($val as $v) {
    if(!isset($output[$v->name])) {
        $output[$v->name]["name"] = $v->name;
        $output[$v->name]["values"] = $v->values;
    } else {
        $output[$v->name]["values"] = array_merge($v->values,$output[$v->name]["values"]);
    }
}
$output = array_values($output);
print_r(json_encode($output));
?>

答案 1 :(得分:0)

array_reduce()可能是您想要的解决方案。

$result = array_values(array_reduce($data, function ($old, $new) {
    $old[$new->name] = $new;
    return $old;
}, []));

Demo

答案 2 :(得分:0)

这是我的代码,我首先在php中创建了此数组,然后使用一些foreach解决了问题,这就是结果

<?php
    //creating source vector
    $source=array();
    array_push($source,["name"=>"Size","values"=> ["Small"]]);
    array_push($source,["name"=>"Colour","values"=> ["Red"]]);
    array_push($source,["name"=>"Size","values"=> ["Large"]]);
    array_push($source,["name"=>"Colour","values"=> ["Green"]]);
    print_r($source);
    //initializind the result vector
    $result=array();
    array_push($result,["name"=>"Size", "values" => []]);
    array_push($result,["name"=>"Colour", "values" => []]);
    $i=0;

    //starting sorting
    foreach($source as $element){
        if($element['name']==="Size"){
            foreach($element["values"] as $subelement){
                array_push($result[0]["values"],$source[$i]["values"][0]);
            }
        }
        else{
            foreach($element["values"] as $subelement){
                array_push($result[1]["values"],$source[$i]["values"][0]);
            }
        }
        $i++;
    }


    //output
    print_r($source);
    print("<br><br>");
    print_r($result);
?>

这是最终的输出

    Array ( 
          [0] => Array ( 
                      [name] => Size 
                      [values] => Array ( 
                                [0] => Small
                                [1] => Large ) ) 
          [1] => Array ( 
                      [name] => Colour 
                      [values] => Array (
                                [0] => Red 
                                [1] => Green ) )  
)

希望有帮助!

答案 3 :(得分:0)

尝试一下:

$newArray = [];
foreach($sourceArray as $element) {
    $newArray[$element->name]['name'] = $element->name;
    $newArray[$element->name]['values'][] = implode($element->values);
}
$newArray = array_values($newArray);