当for循环处于最后一个索引时执行操作 - 1

时间:2015-01-12 14:17:57

标签: php arrays oop

我有一个从其他一些函数中检索特定数据的函数。

我遍历检索到的数据并用#字符分解每个结果,数据如下所示:

未爆炸

string(14) "1#28#92#28#103"
string(17) "551#36#112#36#126"
string(17) "651#45#132#45#150"
string(17) "751#59#159#59#179"
string(17) "851#77#194#91#208"
string(18) "951#99#229#129#245"
string(20) "1051#122#264#166#282"
string(20) "1151#145#300#203#318"
string(20) "1251#168#335#240#355"
string(20) "1351#190#371#278#392"
string(20) "1451#213#406#315#428"
string(20) "1551#236#441#352#465"
string(20) "1651#258#477#389#501"
string(20) "1751#281#512#427#538"
string(20) "1851#304#548#464#575"
string(20) "1951#327#583#501#611"
string(20) "2051#349#618#539#648"
string(20) "2151#372#654#576#685"
string(20) "2251#395#689#613#721"
string(20) "2351#417#725#650#758"
string(20) "2451#440#760#688#795"
string(20) "2551#463#795#725#831"
string(20) "2651#486#831#762#868"
string(20) "2751#508#866#799#905"
string(20) "2851#531#902#837#941"
string(20) "2951#554#937#874#978"
string(21) "3051#576#972#911#1015"
string(22) "3151#599#1008#949#1051"
string(22) "3251#618#1040#982#1084"
string(23) "3351#637#1071#1015#1117"
string(23) "3451#655#1102#1049#1149"
string(23) "3551#674#1133#1082#1182"
string(23) "3651#693#1165#1115#1214"
string(23) "3751#711#1196#1148#1247"
string(23) "3851#730#1227#1181#1279"
string(23) "3951#748#1258#1214#1312"
string(23) "4051#767#1290#1247#1344"
string(23) "4151#785#1321#1280#1377"
string(23) "4251#804#1352#1314#1410"
string(23) "4351#823#1383#1347#1442"
string(23) "4451#841#1415#1380#1475"
string(23) "4551#860#1446#1413#1507"
string(23) "4651#878#1477#1446#1540"
string(23) "4751#897#1508#1479#1572"
string(23) "4851#915#1540#1512#1605"
string(23) "4951#934#1571#1546#1637"

分解

array(5) {
  [0]=>
  string(1) "1"
  [1]=>
  string(2) "28"
  [2]=>
  string(2) "92"
  [3]=>
  string(2) "28"
  [4]=>
  string(3) "103"
}
array(5) {
  [0]=>
  string(3) "551"
  [1]=>
  string(2) "36"
  [2]=>
  string(3) "112"
  [3]=>
  string(2) "36"
  [4]=>
  string(3) "126"
}
array(5) {
  [0]=>
  string(3) "651"
  [1]=>
  string(2) "45"
  [2]=>
  string(3) "132"
  [3]=>
  string(2) "45"
  [4]=>
  string(3) "150"
}
array(5) {
  [0]=>
  string(3) "751"
  [1]=>
  string(2) "59"
  [2]=>
  string(3) "159"
  [3]=>
  string(2) "59"
  [4]=>
  string(3) "179"
}
array(5) {
  [0]=>
  string(3) "851"
  [1]=>
  string(2) "77"
  [2]=>
  string(3) "194"
  [3]=>
  string(2) "91"
  [4]=>
  string(3) "208"
}
array(5) {
  [0]=>
  string(3) "951"
  [1]=>
  string(2) "99"
  [2]=>
  string(3) "229"
  [3]=>
  string(3) "129"
  [4]=>
  string(3) "245"
}
array(5) {
  [0]=>
  string(4) "1051"
  [1]=>
  string(3) "122"
  [2]=>
  string(3) "264"
  [3]=>
  string(3) "166"
  [4]=>
  string(3) "282"
}
array(5) {
  [0]=>
  string(4) "1151"
  [1]=>
  string(3) "145"
  [2]=>
  string(3) "300"
  [3]=>
  string(3) "203"
  [4]=>
  string(3) "318"
}
array(5) {
  [0]=>
  string(4) "1251"
  [1]=>
  string(3) "168"
  [2]=>
  string(3) "335"
  [3]=>
  string(3) "240"
  [4]=>
  string(3) "355"
}
array(5) {
  [0]=>
  string(4) "1351"
  [1]=>
  string(3) "190"
  [2]=>
  string(3) "371"
  [3]=>
  string(3) "278"
  [4]=>
  string(3) "392"
}
array(5) {
  [0]=>
  string(4) "1451"
  [1]=>
  string(3) "213"
  [2]=>
  string(3) "406"
  [3]=>
  string(3) "315"
  [4]=>
  string(3) "428"
}
array(5) {
  [0]=>
  string(4) "1551"
  [1]=>
  string(3) "236"
  [2]=>
  string(3) "441"
  [3]=>
  string(3) "352"
  [4]=>
  string(3) "465"
}
array(5) {
  [0]=>
  string(4) "1651"
  [1]=>
  string(3) "258"
  [2]=>
  string(3) "477"
  [3]=>
  string(3) "389"
  [4]=>
  string(3) "501"
}
array(5) {
  [0]=>
  string(4) "1751"
  [1]=>
  string(3) "281"
  [2]=>
  string(3) "512"
  [3]=>
  string(3) "427"
  [4]=>
  string(3) "538"
}
array(5) {
  [0]=>
  string(4) "1851"
  [1]=>
  string(3) "304"
  [2]=>
  string(3) "548"
  [3]=>
  string(3) "464"
  [4]=>
  string(3) "575"
}
array(5) {
  [0]=>
  string(4) "1951"
  [1]=>
  string(3) "327"
  [2]=>
  string(3) "583"
  [3]=>
  string(3) "501"
  [4]=>
  string(3) "611"
}
array(5) {
  [0]=>
  string(4) "2051"
  [1]=>
  string(3) "349"
  [2]=>
  string(3) "618"
  [3]=>
  string(3) "539"
  [4]=>
  string(3) "648"
}
array(5) {
  [0]=>
  string(4) "2151"
  [1]=>
  string(3) "372"
  [2]=>
  string(3) "654"
  [3]=>
  string(3) "576"
  [4]=>
  string(3) "685"
}
array(5) {
  [0]=>
  string(4) "2251"
  [1]=>
  string(3) "395"
  [2]=>
  string(3) "689"
  [3]=>
  string(3) "613"
  [4]=>
  string(3) "721"
}
array(5) {
  [0]=>
  string(4) "2351"
  [1]=>
  string(3) "417"
  [2]=>
  string(3) "725"
  [3]=>
  string(3) "650"
  [4]=>
  string(3) "758"
}
array(5) {
  [0]=>
  string(4) "2451"
  [1]=>
  string(3) "440"
  [2]=>
  string(3) "760"
  [3]=>
  string(3) "688"
  [4]=>
  string(3) "795"
}
array(5) {
  [0]=>
  string(4) "2551"
  [1]=>
  string(3) "463"
  [2]=>
  string(3) "795"
  [3]=>
  string(3) "725"
  [4]=>
  string(3) "831"
}
array(5) {
  [0]=>
  string(4) "2651"
  [1]=>
  string(3) "486"
  [2]=>
  string(3) "831"
  [3]=>
  string(3) "762"
  [4]=>
  string(3) "868"
}
array(5) {
  [0]=>
  string(4) "2751"
  [1]=>
  string(3) "508"
  [2]=>
  string(3) "866"
  [3]=>
  string(3) "799"
  [4]=>
  string(3) "905"
}
array(5) {
  [0]=>
  string(4) "2851"
  [1]=>
  string(3) "531"
  [2]=>
  string(3) "902"
  [3]=>
  string(3) "837"
  [4]=>
  string(3) "941"
}
array(5) {
  [0]=>
  string(4) "2951"
  [1]=>
  string(3) "554"
  [2]=>
  string(3) "937"
  [3]=>
  string(3) "874"
  [4]=>
  string(3) "978"
}
array(5) {
  [0]=>
  string(4) "3051"
  [1]=>
  string(3) "576"
  [2]=>
  string(3) "972"
  [3]=>
  string(3) "911"
  [4]=>
  string(4) "1015"
}
array(5) {
  [0]=>
  string(4) "3151"
  [1]=>
  string(3) "599"
  [2]=>
  string(4) "1008"
  [3]=>
  string(3) "949"
  [4]=>
  string(4) "1051"
}
array(5) {
  [0]=>
  string(4) "3251"
  [1]=>
  string(3) "618"
  [2]=>
  string(4) "1040"
  [3]=>
  string(3) "982"
  [4]=>
  string(4) "1084"
}
array(5) {
  [0]=>
  string(4) "3351"
  [1]=>
  string(3) "637"
  [2]=>
  string(4) "1071"
  [3]=>
  string(4) "1015"
  [4]=>
  string(4) "1117"
}
array(5) {
  [0]=>
  string(4) "3451"
  [1]=>
  string(3) "655"
  [2]=>
  string(4) "1102"
  [3]=>
  string(4) "1049"
  [4]=>
  string(4) "1149"
}
array(5) {
  [0]=>
  string(4) "3551"
  [1]=>
  string(3) "674"
  [2]=>
  string(4) "1133"
  [3]=>
  string(4) "1082"
  [4]=>
  string(4) "1182"
}
array(5) {
  [0]=>
  string(4) "3651"
  [1]=>
  string(3) "693"
  [2]=>
  string(4) "1165"
  [3]=>
  string(4) "1115"
  [4]=>
  string(4) "1214"
}
array(5) {
  [0]=>
  string(4) "3751"
  [1]=>
  string(3) "711"
  [2]=>
  string(4) "1196"
  [3]=>
  string(4) "1148"
  [4]=>
  string(4) "1247"
}
array(5) {
  [0]=>
  string(4) "3851"
  [1]=>
  string(3) "730"
  [2]=>
  string(4) "1227"
  [3]=>
  string(4) "1181"
  [4]=>
  string(4) "1279"
}
array(5) {
  [0]=>
  string(4) "3951"
  [1]=>
  string(3) "748"
  [2]=>
  string(4) "1258"
  [3]=>
  string(4) "1214"
  [4]=>
  string(4) "1312"
}
array(5) {
  [0]=>
  string(4) "4051"
  [1]=>
  string(3) "767"
  [2]=>
  string(4) "1290"
  [3]=>
  string(4) "1247"
  [4]=>
  string(4) "1344"
}
array(5) {
  [0]=>
  string(4) "4151"
  [1]=>
  string(3) "785"
  [2]=>
  string(4) "1321"
  [3]=>
  string(4) "1280"
  [4]=>
  string(4) "1377"
}
array(5) {
  [0]=>
  string(4) "4251"
  [1]=>
  string(3) "804"
  [2]=>
  string(4) "1352"
  [3]=>
  string(4) "1314"
  [4]=>
  string(4) "1410"
}
array(5) {
  [0]=>
  string(4) "4351"
  [1]=>
  string(3) "823"
  [2]=>
  string(4) "1383"
  [3]=>
  string(4) "1347"
  [4]=>
  string(4) "1442"
}
array(5) {
  [0]=>
  string(4) "4451"
  [1]=>
  string(3) "841"
  [2]=>
  string(4) "1415"
  [3]=>
  string(4) "1380"
  [4]=>
  string(4) "1475"
}
array(5) {
  [0]=>
  string(4) "4551"
  [1]=>
  string(3) "860"
  [2]=>
  string(4) "1446"
  [3]=>
  string(4) "1413"
  [4]=>
  string(4) "1507"
}
array(5) {
  [0]=>
  string(4) "4651"
  [1]=>
  string(3) "878"
  [2]=>
  string(4) "1477"
  [3]=>
  string(4) "1446"
  [4]=>
  string(4) "1540"
}
array(5) {
  [0]=>
  string(4) "4751"
  [1]=>
  string(3) "897"
  [2]=>
  string(4) "1508"
  [3]=>
  string(4) "1479"
  [4]=>
  string(4) "1572"
}
array(5) {
  [0]=>
  string(4) "4851"
  [1]=>
  string(3) "915"
  [2]=>
  string(4) "1540"
  [3]=>
  string(4) "1512"
  [4]=>
  string(4) "1605"
}
array(5) {
  [0]=>
  string(4) "4951"
  [1]=>
  string(3) "934"
  [2]=>
  string(4) "1571"
  [3]=>
  string(4) "1546"
  [4]=>
  string(4) "1637"
}

我想要它,所以每当最后一个索引-1被for循环处理时,代码应该做一个特定的动作,这是我自己的try(函数):

    public function getDataByVehicle(VehicleType $vehicleType, VehicleOwner $vehicleOwner) {
        $data         = $vehicleType->resolveVehicleTypeWithData($vehicleType);
        $dataKey      = $vehicleType->resolveDataKey($data, $vehicleOwner);
        $dataLocation = $this->data->$dataKey;

        if (is_a($vehicleType, "PassengerCar")) {
            for ($dataLocationIndex = 0; $dataLocationIndex < count($dataLocation); $dataLocationIndex++) {
                $formattedData = explode("#", $dataLocation[$dataLocationIndex]);

                var_dump($formattedData);

                //My own try!
                if (!$dataLocationIndex == count($dataLocation)) {
                    // do action
                }
            }

        }
        return null;
    }

问题

  • 当for循环位于最后一个索引 - 1?
  • 时,如何让我的代码执行特定操作

注意

  • 我想使用for循环,因为我希望能够检查数组的下一个索引是什么,如果它不存在,请记住它记住的最后一个。

1 个答案:

答案 0 :(得分:1)

for循环仅在以下情况下运行:$ dataLocationIndex&lt; count($ dataLocation),这意味着你应该检查count($ dataLocation)减去1,因为循环永远不会达到$ dataLocation的计数。

//My own try!
if ($dataLocationIndex === count($dataLocation)-1) {
    // do action
}

希望它有效。