Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何转到数组中的下一个索引?_Php - Fatal编程技术网

Php 如何转到数组中的下一个索引?

Php 如何转到数组中的下一个索引?,php,Php,我有一个搜索表单,可以生成搜索结果。每个结果都有一个与mysql中的id相对应的标识。为了获得每个mysql字段的完整结果,我使用了一个指向fullresult.php?id=569的链接 在检索结果时,我将所有对应的ID放在一个$\u会话['ID']中,如下所示: array(41) { [0]=> string(3) "569" [1]=> string(4) "1085" [2]=> string(3) "289" [3]=> st

我有一个搜索表单,可以生成搜索结果。每个结果都有一个与mysql中的id相对应的标识。为了获得每个mysql字段的完整结果,我使用了一个指向fullresult.php?id=569的链接

在检索结果时,我将所有对应的ID放在一个$\u会话['ID']中,如下所示:

 array(41) {
  [0]=>
  string(3) "569"
  [1]=>
  string(4) "1085"
  [2]=>
  string(3) "289"
  [3]=>
  string(3) "221"
当我在fullresult.php?id=569显示的页面上时,我需要创建一个指向fullresult.php?id=1085的链接

试图通过$_会话['id']循环,试图获取当前索引并转到下一个索引似乎不起作用。或者至少我不知道怎么做

我现在这样做:

$id_now = $_GET['id'];
$liste_ids = $_SESSION['ids'];
if( ($ida_now = current($liste_ids)) !== FALSE){
    $nxt = next($liste_ids);
    echo$nxt; 
}
问题是当前($liste_id)始终是569(第一个索引=>$liste_id中的值),因此$nxt始终是1085


当我使用fullresult.php?id=1085时,我要做的是检索**下一行**的$liste_id,以便从$liste_id中检索289,依此类推。

只要

// Make sure the session is started
if(!session_id())
    session_start();

// Make sure the user entered the id on the URL
if(empty($_GET["id"]))
    die("Please enter the id of the result");

$resultId = $_GET["id"];

// Validate the id
if(!is_numeric($resultId))
    die("Invalid id, it most be an integer");

if(empty($_SESSION["ids"]))
    die("No ids to follow");

// First the index of the $resultId
$resultIndex = array_search($resultId, $_SESSION["ids"]);

if($resultIndex === false)
    die("Couldn't find ". $resultId);

$resultIndex++;

if(empty($_SESSION["ids"][$resultIndex]))
    die("Reached end");
else
    echo "Your link is " . "/fullresult.php?id=" . $_SESSION["ids"][$resultIndex];
  • 您有一个唯一的数组
  • get
    参数获取数组中的值
  • 您的数组具有所有索引
如果您确定所有数组索引都存在,那么这将变得更加容易。那就是,

$needle = $_GET['id']; //id in your url has value, not index.
$index = array_search('$needle', $array);//get the index.
$value = $array($index+1);
echo $value;
一般来说,当所有数组索引都存在时,可以使用该索引

$needle = $_GET['id']; 
$index = array_search('$needle', $array);
while (key($List) !== $index){ 
$value = next($index);
}
echo $value;

请测试代码,因为我还没有测试它。此外,采取任何必要的防御措施。与用户手动将url更改为不在数组中的值类似。依此类推。

因此,您通过
$\u GET
收到一个id,并希望打印一个带有数组中下一个id的链接,对吗?同时展示你的尝试。我现在在这里看到三件事
id
ida
idas
?谢谢Kishor。更正。在本例中,应该只有id和liste_id,$resultIndex是当前索引。我正在寻找下一个值…只需将其更改为
$\u SESSION['id'][$resultIndex+1]
,它就可以工作了。另外,验证密钥是否存在也很酷。