使用父数组值作为键在foreach循环中创建多维数组

时间:2019-06-07 13:28:46

标签: php arrays

我有一个如下数组,从中我用后值作为新数组的键来创建新数组

Array
(
    [0] => stdClass Object
        (
            [name] => Max right
            [uid] => 148
            [post] => Sales
            [post_id] => 870
            [score] => 2.3333
        )

    [1] => stdClass Object
        (
            [name] => Max right
            [uid] => 148
            [post] => Dev
            [skill_id] => 881
            [score] => 3.5
        )

    [10] => stdClass Object
        (
            [name] => Ron Deck
            [uid] => 118
            [post] => Sales
            [skill_id] => 870
            [score] => 2
        )

    [11] => stdClass Object
        (
            [name] => Ron Deck
            [uid] => 118
            [post] => Dev
            [skill_id] => 881
            [score] => 4
        )

    [20] => stdClass Object
        (
            [name] => MIndy Sane
            [uid] => 16
            [post] => Sales
            [post_id] => 870
            [score] => 3
        )

    [21] => stdClass Object
        (
            [name] => MIndy Sane
            [uid] => 16
            [post] => Dev
            [post_id] => 881
            [score] => 5
        )

)

我试图按如下所示按其uid分组以获取所需的输出

$records = [];
        foreach ($data as $out_data)
        {
            $records[$out_data->uid][] = $out_data->score ;
            $records[$out_data->uid][] = $out_data->name ;
            $records[$out_data->uid][] = $out_data->post_id ;
            $records[$out_data->uid][] = $out_data->post ;
        }

并无法确定正确的方法如下获取输出。基本上在主数组中,我有一些必须用作键但无法找到方法的值

Array
(
    [148] => Array
        (
            [0] => Max right
            [1] => 2.3333
            [2] => 870
            [3] => Sales
            [4] => Max right
            [5] => 3.5
            [6] => 881
            [7] => Dev
        )
    [16] => Array
        (
            [0] => Ron Deck
            [1] => 2
            [2] => 870
            [3] => Sales
            [4] => Ron Deck
            [5] => 4
            [6] => 881
            [7] => Dev
        )
    [121] => Array
        (
            [0] => MIndy Sane
            [1] => 3
            [2] => 870
            [3] => Sales
            [4] => Ron Deck
            [5] => 5
            [6] => 881
            [7] => Dev
        )   

我的预期输出如下

$records = array(
            array('name'=> 'Max right', 'Sales' =>2.3333, 'Dev'=>3.5),
            array('name'=> 'Ron Deck', 'Sales' =>2, 'Dev'=>4),
            array('name'=> 'MIndy Sane', 'Sales' =>3, 'Dev'=>5),
        ); 

3 个答案:

答案 0 :(得分:1)

修正循环:

$records = []
foreach ($data as $out_data) {
    if (empty($records[$out_data->uid])) {
        $records[$out_data->uid] = [
            'name' => $out_data->name;
        ];
    }

    $records[$out_data->uid][$out_data->post] = $out_data->score;
}

答案 1 :(得分:0)

您几乎拥有了它。使用$out_data->post命名密钥。显然,这是假设每个人都不会重复此操作:

foreach ($data as $out_data)
{
    $records[$out_data->uid]['name'] = $out_data->name;
    $records[$out_data->uid][$out_data->post] = $out_data->score;
}

然后根据需要重新索引(不需要):

$records = array_values($records);

答案 2 :(得分:0)

您可以将foreacharray_key_exists一起使用

$res = [];
foreach($arr as $key => $val){
  if(array_key_exists($val['uid'], $res)){
    $res[$val['uid']][$val['post']] =  $val['score'];
  }else{
    $res[$val['uid']] = [
                'name'       => $val['name'],
                $val['post'] => $val['score']
    ];
  }
}

Live example