从循环中获取最后一个数字,将其作为数组中的索引引用


Get last number from a loop to reference it as an index in an array

我想从$max中检索数组中的最后一个元素,并在数组$set_of_path中引用它。我只想要最后一个值。这段代码的问题是这个$max值,它将值0打印到文件末尾,因此没有输出最后一个值。。每个元素都实现了。。有人能帮帮我吗。提前谢谢。

for($ind=0;$ind <count($array_file[0]['pair']);$ind++) {
    ....
    ...
if((($city)===$initial or ($city[0]===$initial))){
                    $x =$array_file[0]['pair'][$ind]['city'];//display all cities
                    if ((end($x))===$final){ 
                        //push $array_file content into the array
                        $xd[]=$array_file[0]['pair'][$ind];
                        $count=count(end($xd));
                            $last_element = array();
                            $a[] = array_merge($xd);
                            $end = count(end($a));
                            $push = array_push($last_element,$end);
                            $max = max($last_element)-1;
                            print_r($max);
                            $set_of_path=@$a[$max];
                            print_r($set_of_path);  
                            print_r('<pre>');
                    }
                    };
...
...
.
...
.
..

使用SizeOf获取数组中项目的计数。

http://php.net/manual/en/function.sizeof.php

解决方案

<?php
$i = 0;
//Process array in loop
//increment $i++; or $i = sizeof($array);
?>