从代码一维/二维数组中删除重复的代码

我目前在使用包含其他数组的数组时遇到问题。下面是我当前的代码。但是问题是我执行了100次相同的代码,唯一的区别是当我遍历要循环的Array内的数组时需要的$ anotherOtherIndex。

如果有人可以通过消除重复的代码来减少所需的代码量,这将对我有很大帮助。

for( $index = 0; $index < $endOfLoop; $index++ ) {        
            if(true) {
                $myVariable = arrayWithArrays[$index][$anotherIndex]
                x100...
            }else{
                $myVariable = arrayWithArrays[$index][$anotherIndex][$anotherOtherIndex]
                x100...
}

伪代码解决方案(那是完美的,现在我们都不完美了;)):

for( $index = 0; $index < $endOfLoop; $index++ ) {        
            $myVariable = arrayWithArrays[$index][$anotherIndex][?????]
}


michaelwong2009 回答:从代码一维/二维数组中删除重复的代码

您可以设置主要部分,并且仅在满足条件的情况下进一步进入数组

for( $i = 0; $i < $endOfLoop; $i++ ) {        
    $myVariable = arrayWithArrays[$index][$anotherIndex]

    if(false) {
        $myVariable = $myVariable[$anotherOtherIndex]
    }
}
本文链接:https://www.f2er.com/2597188.html

大家都在问