需要php数组帮助-如果当前数组项=';最后一项或第一项';然后';做点什么';
需要一些关于这个脚本的帮助。这是工作,但我想有一个方法来简化它一点 现在我有了“if($currentPageIndex==4)”,每次我从projectlist数组中添加或删除一个项时,我都必须手动更新“4”。我需要的是说‘如果($currentPageIndex==数组的最后一项),这样我就可以添加/删除项目,而不用担心更新数字 我该怎么做?我已经阅读了各种各样的选择,并且一直在尝试,但到目前为止运气都不好 如果可能的话,该解决方案是否也可用于上一个和下一个链接? 因此,它不再是+4和-4,而是分别跳到第一项和最后一项 非常感谢您的帮助 在此进行工作演示: 代码如下:需要php数组帮助-如果当前数组项=';最后一项或第一项';然后';做点什么';,php,arrays,Php,Arrays,需要一些关于这个脚本的帮助。这是工作,但我想有一个方法来简化它一点 现在我有了“if($currentPageIndex==4)”,每次我从projectlist数组中添加或删除一个项时,我都必须手动更新“4”。我需要的是说‘如果($currentPageIndex==数组的最后一项),这样我就可以添加/删除项目,而不用担心更新数字 我该怎么做?我已经阅读了各种各样的选择,并且一直在尝试,但到目前为止运气都不好 如果可能的话,该解决方案是否也可用于上一个和下一个链接? 因此,它不再是+4和-4,
<?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>
-
-
使用
要获得所需的数字,请使用
要获取所需的数目,请使用获取数组的元素数目
使用获取数组的元素数
试试
$projectlist[count($projectlist)-1]
试试$projectlist[count($projectlist)-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>
-
-
我使用以下代码和一位朋友的帮助,成功地使它完全工作。
非常感谢你的帮助
这里似乎使用了各种提到的步骤。使用@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>
-
-
我通常更喜欢使用end()
方法获取最后一个元素,因为使用计数假设数组未修改。如果某个元素在某个点被删除,那么让您保留索引0、1、3和4,count(…)-1
将产生3,这不是最后一个元素。和end()
可用于非数字索引。不过,在本例中,您将索引与实际元素进行比较。@Wiseguy很酷,但您不应该使用这样的稀疏数组,除非它们用作带数字键的关联数组。对于顺序数组,您总是需要顺序索引,例如,使用array\u splice
删除时可以强制执行。我同意您不需要数字索引中的间隙,但如果有人确实在某处删除元素,则使用count()
进行猜测将是不准确的。当然,您总是可以使用array\u values()
将索引重置为序列号。我通常更喜欢使用end()
方法来获取最后一个元素,因为使用计数假设数组未修改。如果某个元素在某个点被删除,那么让您保留索引0、1、3和4,count(…)-1
将产生3,这不是最后一个元素。和end()
可用于非数字索引。不过,在本例中,您将索引与实际元素进行比较。@Wiseguy很酷,但您不应该使用这样的稀疏数组,除非它们用作带数字键的关联数组。对于顺序数组,您总是需要顺序索引,例如,使用array\u splice
删除时可以强制执行。我同意您不需要数字索引中的间隙,但如果有人确实在某处删除元素,则使用count()
进行猜测将是不准确的。当然,您可以使用array\u values()
将索引重置为序列号。
if( $currentPageIndex === end($projectlist) ) {
//sup
}
<?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>