2012-06-07 36 views
1

需要对这个脚本有一点帮助。它正在工作,但我想要一种简化它的方法。php array help required - if current array item ='last or first item'then'do something'

此刻我有'if($ currentPageIndex == 4)'我必须手动更新'4'每次我添加或删除项目列表数组中的项目。我需要的是说'如果($ currentPageIndex == 数组的最后一项)'这样我可以添加/删除项目,而不必担心更新数字。

我应该怎么办?我已经阅读了各种选项,并且迄今为止没有运气尝试过。

此外,如果可能的话,解决方案可用于上一个&下一个链接以及? 因此,它不是+ 4-4它分别跳转到第一个和最后一个项目。

任何帮助非常感谢。

这里工作演示:http://www.ok-hybrid.com/projects/monkey/ 代码在这里:

<?php 

$currentPath = explode('?', $_SERVER['REQUEST_URI']); //make sure we don't count any GET variables! 
$currentPath = $currentPath[0]; //grab just the path 
$projectlist = array(
     '/projects/monkey/', 
     '/projects/tiger/', 
     '/projects/parrot/', 
     '/projects/banana/', 
     '/projects/aeroplane/', 
); 



if(! in_array($currentPath, $projectlist)) { 
    die('Not a valid page!'); //they didn't access a page in our master list, handle error here. 
} 

$currentPageIndex = array_search($currentPath, $projectlist); 

if($currentPageIndex > 0) { 
    $prevlink = '<a href="'.$projectlist[$currentPageIndex-1].'">Prev</a>'; 
} else { 
    $prevlink = '<a href="'.$projectlist[$currentPageIndex+4].'">Prev</a>'; 
    } 

if($currentPageIndex == 4) { 
    $nextlink = '<a href="'.$projectlist[$currentPageIndex-4].'">Next</a>'; 
} else { 
    $nextlink = '<a href="'.$projectlist[$currentPageIndex+1].'">Next</a>'; 
} 

?> 



<ul id="sub-nav"> 

<li> 
<?php 
print_r($prevlink); 
?> 
</li> 

<li> 
<?php 
print_r($nextlink); 
?> 
</li> 

</ul> 

回答

2

使用

sizeof($projectList) - 1 

,以获得所需数量

2

让您的阵列count()的元素的数量。

if ($currentPageIndex == count($projectlist)-1) { 
    // do something 
} 
3

尝试$projectlist[count($projectlist)-1]

1
if($currentPageIndex === end($projectlist)) { 
    //sup 
} 
+0

我通常更喜欢'end()'方法来获取最后一个元素,因为使用count假定数组未被修改。如果一个元素在某个时候被删除,比如说让你的索引为0,1,3和4,那么count(...) - 1将产生3,这不是最后一个元素。 'end()'与非数字索引一起工作。不过,在这种情况下,您正在比较索引与实际元素。 – Wiseguy

+0

@Wiseguy酷,但你不应该有像这样的稀疏数组,除非它们被用作带数字键的关联数组。对于顺序数组,您总是需要连续的索引,例如,在使用'array_splice'移除时可以执行这些索引。 – Esailija

+0

我同意你不会在数字索引中留下空白,但是如果有人在某处删除某个元素,使用count()来猜测将会不准确。当然,您可以使用'array_values()'将索引重置为连续数字。 – Wiseguy

1

我设法使用下面的代码和一个朋友的帮助下它完全工作。 非常感谢所有的帮助。

似乎在这里使用各种提到的步骤。使用@Arthur提到的'sizeof($ projectList) - 1'来检查链接。

<?php 

    $currentPath = explode('?', $_SERVER['REQUEST_URI']); //make sure we don't count any GET variables! 
    $currentPath = $currentPath[0]; //grab just the path 



    $projectlist = array(

    '/projects/monkey/', 
    '/projects/tiger/', 
    '/projects/parrot/', 
    '/projects/banana/', 
    '/projects/aeroplane/', 
    '/projects/egg/', 

    ); 





    if(! in_array($currentPath, $projectlist)) { 
     die('Not a valid page!'); //they didn't access a page in our master list, handle error here 
    } 

    $currentPageIndex = array_search($currentPath, $projectlist); 

    //echo $currentPageIndex."<Br />"; 


    //items in array count 
    $projectlist_count = count($projectlist); 


    if($currentPageIndex > 0) { //make sure it's not the first page 
     $prevlink = '<a href="'.$projectlist[$currentPageIndex-1].'">Prev</a>'; 
      } else { 
     $prevlink = '<a href="'.$projectlist[$projectlist_count-1].'">Prev</a>'; 
      } 



    if($nextlink < sizeof($projectlist)-1) { //make sure we're not the last page 

    if($currentPageIndex+1 >= $projectlist_count) 
    { 
    //go back to start of array 
     $nextlink = '<a href="'.$projectlist[0].'">Next</a>'; 
    } else { 
     $nextlink = '<a href="'.$projectlist[$currentPageIndex+1].'">Next</a>'; 
    } 
    } 


    ?> 

    <ul id="sub-nav"> 

    <li> 
    <?php 
    print_r($prevlink); 
    ?> 
    </li> 

    <li> 
    <?php 
    print_r($nextlink); 
    ?> 
    </li> 

    </ul>