利用PHP的数组函数优化值对象

利用PHP的数组函数优化值对象,php,arrays,Php,Arrays,我在值对象中有以下两种方法: public final function unlockableLetters() { return [ [], ['A', 'B'], ['C'] ][$this->level]; } public final function unlockedLetters() { return [ [], ['A', 'B'], ['A', 'B',

我在值对象中有以下两种方法:

public final function unlockableLetters()
{
    return [
        [],
        ['A', 'B'],
        ['C']
    ][$this->level];
}

public final function unlockedLetters()
{
    return [
        [],
        ['A', 'B'],
        ['A', 'B', 'C'],
    ][$this->level];
}
第一个返回每个级别的可解锁元素

在级别0时,返回[]。 在第1级返回A和B。 在2级返回C。 第二个返回未锁定的元素

在级别0时,返回[]。 在级别1返回A和B.//NULL+A+B 级别2返回A、B和C.//A+B+C 有一种方法可以使用php的数组函数创建从索引0到当前$this->level的unlockLetters返回的数组的合并

编辑
我只想在unlockableLetters方法中留下字母。所以,unlockletter可以从第一个方法构建自己的返回值。如果前者发生变化,后者也会发生变化。

你想要什么还不完全清楚。正如我在评论中所说:

同意@deceze,不清楚要合并什么。。。你是说你想用数组函数而不是硬编码的方法从unlockLetters生成unlockLetters的返回?另外,我认为这里应该有更好的方法名,因为这也会让人困惑。。。我会使用getUnlockableLetters和getUnlockableLetters或者其他类似的东西,这取决于它们的使用方式

然而,我认为你的意思可能是这样的:

// add a property so we can reference it - you might want to adjust
// the visibility to private or public depending on its nature
protected $unlockableLetters = [
    [],
    ['A', 'B'],
    ['C']
];

public final function unlockLetters()
{
    // just return the reference to the letters for the level
    return $this->unlockableLetters[$this->level];
}

public final function unlockedLetters()
{
    // slice off the array of latters from the lowest level 
    // to the current one - note the +1 since array_slice
    // wants a length and our offsets are 0-based and thus length-1
    $data = array_slice($this->unlockableLetters, 0, $this->level+1);

    // use array reduce to build a single array that has
    // all the letters for levels 0 --> $this->level
    return array_reduce($data, function ($merged, $datum) {
       return array_merge($merged, $datum);
    }, []);
}

你想要什么还不完全清楚。正如我在评论中所说:

同意@deceze,不清楚要合并什么。。。你是说你想用数组函数而不是硬编码的方法从unlockLetters生成unlockLetters的返回?另外,我认为这里应该有更好的方法名,因为这也会让人困惑。。。我会使用getUnlockableLetters和getUnlockableLetters或者其他类似的东西,这取决于它们的使用方式

然而,我认为你的意思可能是这样的:

// add a property so we can reference it - you might want to adjust
// the visibility to private or public depending on its nature
protected $unlockableLetters = [
    [],
    ['A', 'B'],
    ['C']
];

public final function unlockLetters()
{
    // just return the reference to the letters for the level
    return $this->unlockableLetters[$this->level];
}

public final function unlockedLetters()
{
    // slice off the array of latters from the lowest level 
    // to the current one - note the +1 since array_slice
    // wants a length and our offsets are 0-based and thus length-1
    $data = array_slice($this->unlockableLetters, 0, $this->level+1);

    // use array reduce to build a single array that has
    // all the letters for levels 0 --> $this->level
    return array_reduce($data, function ($merged, $datum) {
       return array_merge($merged, $datum);
    }, []);
}

只要我理解你的问题,你就是在找这个

public final function unlockableLetters() {
    $unlockables = [
        [],
        ['A', 'B'],
        ['C']
    ];
    return array_reduce(array_slice($unlockables, 0, $this->level + 1), function($carry, $item){
        return array_merge($carry, $item);
    }, []);
}

只要我理解你的问题,你就是在找这个

public final function unlockableLetters() {
    $unlockables = [
        [],
        ['A', 'B'],
        ['C']
    ];
    return array_reduce(array_slice($unlockables, 0, $this->level + 1), function($carry, $item){
        return array_merge($carry, $item);
    }, []);
}

我同意其他开发者的观点。 问题不清楚。 但我的猜测是:

public final function unlockLetters()
{
    $arr =  [
        [],
        ['A', 'B'],
        ['C']
        ];
    return $arr[$this->level];
}

public final function unlockedLetters()
{
    $arr = [
        [],
        ['A', 'B'],
        ['A', 'B', 'C']
    ];
    return $arr[$this->level];
}

我同意其他开发者的观点。 问题不清楚。 但我的猜测是:

public final function unlockLetters()
{
    $arr =  [
        [],
        ['A', 'B'],
        ['C']
        ];
    return $arr[$this->level];
}

public final function unlockedLetters()
{
    $arr = [
        [],
        ['A', 'B'],
        ['A', 'B', 'C']
    ];
    return $arr[$this->level];
}

你花了很多时间准备你的问题,但你的实际问题有点简练,难以理解。就我个人而言,我不清楚你在问什么。我同意@deceze,不清楚你想合并什么。。。你是说你想用数组函数而不是硬编码的方法从unlockLetters生成unlockLetters的返回?另外,我认为这里应该有更好的方法名,因为这也会让人困惑。。。我会使用GetUnlockLetters和GetUnlockLetters或者其他类似的东西,具体取决于它们的使用方式。我已经将UnlockLetters编辑为UnlockLetters。这一点更加明确。谢谢你花了很多时间准备你的问题,但是你的实际问题有点简练,很难理解。就我个人而言,我不清楚你在问什么。我同意@deceze,不清楚你想合并什么。。。你是说你想用数组函数而不是硬编码的方法从unlockLetters生成unlockLetters的返回?另外,我认为这里应该有更好的方法名,因为这也会让人困惑。。。我会使用GetUnlockLetters和GetUnlockLetters或者其他类似的东西,具体取决于它们的使用方式。我已经将UnlockLetters编辑为UnlockLetters。这一点更加明确。谢谢好的,对不起。我已经更新了这个问题。关键是,如果有什么变化,我不想同时改变这两种方法。我只想在可解锁的信件中留下$arr。好的,对不起。我已经更新了这个问题。关键是,如果有什么变化,我不想同时改变这两种方法。我只想在unlockableLetters中留下$arr。