PHP reindex数组方法

时间:2016-12-13 10:42:34

标签: php arrays

我有:

required init(coder aDecoder: NSCoder) {

    islogin = aDecoder.decodeBool(forKey: "islogin")
    forOcType = aDecoder.decodeInteger(forKey: "forOcType") as! Int
    if let temp_type = aDecoder.decodeInteger(forKey: "type") as? Int {

        type = UserType(rawValue: temp_type )
    }else {

        type = nil
    }

    username = aDecoder.decodeObject(forKey: "username") as! String
    password = aDecoder.decodeObject(forKey: "password") as! String
    userId = aDecoder.decodeObject(forKey: "userId") as! String


}

哪种方法效果很好,但是我想知道是否有短版本或内置函数可以做同样的事情?

1 个答案:

答案 0 :(得分:0)

您可以将array_combinearray_column一起使用:

$result = array_combine(
    array_column($filters, 'field'),
    $filters
);

这会将'field'个元素留在数组中。如果要删除它,可以使用array_map

$result = array_combine(
    array_column($filters, 'field'),
    array_map(function ($filter) {
        return [
            'operator' => $filter['operator'],
            'value' => $filter['value']
        ];
    }, $filters)
);

为了简单起见,我直接访问了密钥,但在生产中,您应该始终检查该密钥是否存在于数组中。