如何使用CakePHP HASH类转换数组?

时间:2015-03-12 13:59:29

标签: arrays cakephp cakephp-2.3

我正在开发CakePHP。最近我开始使用CakePHP哈希类。我想使用Hash类函数转换数组格式。 我有以下数组:

$arr = array(
    0 => array(
        'key1' => array(
            'id' => 6
            'type' => insert
            'field_id' => 2
            'activity' => table
            ),
            'key2' => array(
            0 => array(
                'id' =>     
                'key1_id' => 6
            )
            1 => array(
                'id' =>     
                'key1_id' => 6
                )
            )
    )
    1 => array(
            'key1' => array(
            'id' => 5
            'type' => edit
            'field_id' => 3
            'activity' => list
            ),
            'key2' => array(
            0 => array(
                'id' =>     
                'key1_id' => 5
                )
            1 => array(
                'id' =>     
                'key1_id' => 5
            )
          )
    )
    2 => array(
            'key1' => array(
            'id' => 4
            'type' => insert
            'field_id' => 2 
            'activity' => table
            ),
            'key2' => array(
            0 => array(
                'id' =>     
                'key1_id' => 4
                )
            )
    )
    3 => array(
        'key1' => array(
            'id' => 3
            'type' => insert
            'field_id' => 3
            'activity' => list
            ),
            'key2' => array(
            0 => array(
                'id' =>     
                'key1_id' => 3
                )   
        )
    )
    4 => array(
            'key1' => array(
            'id' => 2
            'type' => edit 
            'field_id' => 1
            'activity' => list
        ),
            'key2' => array(
            0 => array(
                'id' =>     
                'key1_id' => 2
                )   
        )
    )
    5 => array(
            'key1' => array(
            'id' => 1
            'type' => edit
            'field_id' => 3
            'activity' => list
            ),
            'key2' => array(
            0 => array(
                'id' =>     
                'key1_id' => 1
                )   
        )
    )

);

条件是任何具有类型 field_id 活动的key1将导致所有key2 合并在一起,列表中稍后出现的key1将被取消设置。 我想将其转换为以下格式:

$arr = array(
    0 => array(
        'key1' => array(
            'id' => 6
            'type' => insert
            'field_id' => 2
            'activity' => table
            ),
            'key2' => array(
            0 => array(
                'id' =>     
                'key1_id' => 6
            )
            1 => array(
                'id' =>     
                'key1_id' => 6
                )
            2 => array(
                'id' =>     
                'key1_id' => 4
                )

            )
    )
    1 => array(
            'key1' => array(
            'id' => 5
            'type' => edit
            'field_id' => 3
            'activity' => list
            ),
            'key2' => array(
            0 => array(
                'id' =>     
                'key1_id' => 5
                )
            1 => array(
                'id' =>     
                'key1_id' => 5
            )
            2 => array(
                'id' =>     
                'key1_id' => 1
            )   
          )
    )

    3 => array(
        'key1' => array(
            'id' => 3
            'type' => insert
            'field_id' => 3
            'activity' => list
            ),
            'key2' => array(
            0 => array(
                'id' =>     
                'key1_id' => 3
                )   
        )
    )
    4 => array(
            'key1' => array(
            'id' => 2
            'type' => edit 
            'field_id' => 1
            'activity' => list
        ),
            'key2' => array(
            0 => array(
                'id' =>     
                'key1_id' => 2
                )   
        )
    )
);

如果你看一下'2'的值 key2 <2> 合并,键'0' '2' 未设置。 基本上应该取消设置缺少的键值。我想知道可以获得数组格式的最佳方式。

1 个答案:

答案 0 :(得分:0)

你想要做的事情有点棘手。我建议通过尝试一些不同的CakePHP sql查询来实现这一点。

<强>解决方案

$keys = Hash::extract($arr, '{n}.key1.field_id');
$vals = Hash::extract($arr, '{n}');
$result = $this->array_combine_($keys, $vals);


function array_merge_multiple(&$v) {
 $z1 = array();
 $z2 = array();
 $len = count($v);
 for ($i = 0; $i < $len; $i++) {
     if ($i < ($len - 1)) {
         $z1 = array_merge($v[$i]['key2'], $v[$i + 1]['key2']);
         $z2 = array_merge($v[$i]['key1'], $v[$i + 1]['key1']);
     } else if($len==1){
         $z1 = $v[$i]['key2'];
         $z2 = $v[$i]['key1'];
     }
     unset($v[$i]);
 }
 $v['key1'] = $z1;
 $v['key2'] = $z2;
}

/* Modified array_combine function ( Hash::combine uses array_combine internally */
function array_combine_($keys, $values) {
    $result = array();
    foreach ($keys as $i => $k) {
        $result[$k][] = $values[$i];
    }
    array_walk($result, 'array_merge_multiple');
    return $result;
}