如何在php中使用锚点将数组传递给另一个页面

时间:2013-02-20 12:28:19

标签: php

我必须使用<a href>将数组值从一个php页面传递到另一个php页面...这是我的编码

$cluster是一个数组

echo "<td><a href=myebon.php&cluster[]=".$cluster.">Click here to OFF</a></td>";
myebon.php

中的

$n=count($_GET[cluster]);
for($i=0;$i<=$n;$i++)
{
    echo $cluster[$i]=$_GET['cluster'][$i];
}

在第二页中无法访问该值,它显示为数组但不显示值。我也尝试过序列化概念......

6 个答案:

答案 0 :(得分:5)

这是明显的示例代码

first.php

<?php
$Mixed = array("1","2","3");
$Text = json_encode($Mixed);
$RequestText = urlencode($Text);
?>
<a href="second.php?cluster=<?php echo $RequestText; ?>">Click</a>

second.php

<?php
$Text = urldecode($_REQUEST['cluster']);
$Mixed = json_decode($Text);
print_r( $Mixed);
?>

我已经检查过,并且工作正常。

答案 1 :(得分:4)

使用http_build_query

$data = array('foo', 'bar', 'lol');

echo '<a href="myebon.php?' . http_build_query(array('cluster' => $data)) . '">link</a>';

输出

<a href="myebon.php?cluster%5B0%5D=foo&cluster%5B1%5D=bar&cluster%5B2%5D=lol">link</a>

可以使用$_GET['cluster']检索它,例如:

foreach ($_GET['cluster'] as $val) { 
    // my work here, example:
    echo $val , "\n";
}

答案 2 :(得分:1)

$string = serialize($array);

然后传递字符串,在其他页面中使用

$array = unserialize($string);

试试这个:

echo "<td><a href=myebon.php&cluster=".serialize($cluster).">Click here to OFF</a></td>";

答案 3 :(得分:0)

我建议你传递数组如下:

//QueryString Variable
$qs = '';
foreach($cluster as $cl)
    $qs .= "&cluster[] =".$cl;

然后在链接构建中使用此变量:

echo "<td><a href=myebon.php?a=1".$qs.">Click here to OFF</a></td>";

答案 4 :(得分:0)

将JSON_ENCODE与URLENCODE一起使用

第一

$Text = json_encode(array('foo', 'bar', 'lol'));
$RequestText =urlencode($Text);

然后

echo "<td><a href=myebon.php&cluster=".$RequestText.">Click here to OFF</a></td>";

第二名:myebon.php

$Text = urldecode($_REQUEST['cluster']);
$Mixed = json_decode($Text);

这样可以正常工作。

答案 5 :(得分:0)

我的第一页编码

$Text = json_encode($cluster);
$RequestText =urlencode($Text);
echo '<table>';
for($i=0;$i<$n;$i++)
{
 for($x=0;$x<=$c1;$x++)
 {
     if($cluster[$i]==$ebclus[$x])
     {    

         echo "<tr><td>".$cname[$i]."</td><td>".$cluster[$i]."</td><td>".$statusid[$x]."</td>";
         if($statusid[$x]==ON)
         {
             echo "<td><a href=myebon.php?&dept=".$dept."&branch=".$branch."&end=".$c1."&start=".$i."&cluster=".$RequestText.">Click here to OFF</a></td>";
         }  
         elseif($statusid[$x]==OFF)
         {
             echo "<td><a href=myebon.php?&dept=".$dept."&branch=".$branch."&end=".$c1."&start=".$i."&cluster=".$RequestText.">Click here to ON</a></td>";
         }
     }
 }   
}

myebon.php

$dept=$_GET[dept];

$branch=$_GET[branch];
$end=$_GET[end];
$start=$_GET[start];
$Text = urldecode($_REQUEST['cluster']);
$Mixed = json_decode($Text);
echo $Mixed;
for($i=0;$i<=$end;$i++)
{
    echo $Mixed[$i];
}

答案显示为[\“73 \”,\“71 \”,\“73 \”]