0

我正在使用 urlencode 和 urldecode 通过 html 表单传递变量。

$info = 'tempId='.$rows['tempID'].'&tempType='.$rows['tempType'].'&dbId='.$rows['ID'];
echo '<input type="hidden" name="rank[]" value="'.urlencode($info).'" >';

这是 $rows 中的内容

array (size=4)
  'ID' => string '110' (length=3)
  'tempID' => string '1' (length=1)
  'tempType' => string 'temp_first' (length=10)
  'pageOrder' => string '0' (length=1)

所以 $info 是

tempId=1&tempType=temp_first&dbId=110

但是如果我再解码它,它会丢失 1 个参数。这怎么可能?

foreach (explode('&', urldecode($list[$i])) as $chunk) {
    $param = explode("=", $chunk);

    $tempId = urldecode($param[0]); // template id
    $tempType = urldecode($param[1]); // Template type
    $dbId = urldecode($param[2]); // database ID

    var_dump($param);

}

输出:

array (size=2)
  0 => string 'dbId' (length=4)
  1 => string '110' (length=3)

有时,数组中甚至有一些不应该存在的东西,例如它不是 temp_first,而是 tempType。只是变量名。

我希望你们能帮助我

4

5 回答 5

3

无需手动分解和处理字符串,您可以使用parse_str()

parse_str(urldecode($list[$i]), $output);
var_dump($output);

会输出:

array
  'tempId' => string '1' (length=1)
  'tempType' => string 'temp_first' (length=10)
  'dbId' => string '110' (length=3)
于 2013-04-29T09:09:45.693 回答
2

try this

  $result=array();
  foreach (explode('&', urldecode($list[$i])) as $chunk) {
     $param = explode("=", $chunk);
     $result[$param[0]]=$param[1];
  } 
  var_dump($result);
于 2013-04-29T09:06:04.910 回答
0

Could you try this and check the result (I'm groping in the dark though):

//change this <input type="hidden" name="rank[]" value="'.urlencode($info).'" > to
//<input type="hidden" name="urlargs" value="'.urlencode($info).'" >
$values = explode('&',urldecode($_POST['urlargs']));
$arguments = array();
foreach($values as $argument_set){
    $data = explode('=',$argument_set);
    $arguments[$data[0]] = $data[1];
}
var_dump($arguments);

I believe the problem is in the way you're processing the value

于 2013-04-29T09:07:15.510 回答
0
$data=array();
foreach (explode('&', urldecode($list[$i])) as $chunk) {

    $param = explode("=", $chunk); //
    $data[$param[0]]=$param[1]

}
于 2013-04-29T09:07:43.517 回答
0

不要将所有代码组合在一起,而是首先将其放在单独的变量中并回显内容以进行调试。因为您说您丢失了一个变量,但您显示的输出只是变量之一。另外两个的var_dump是什么?

因为你var_dump($param);会输出'='之前和'='之后的部分,所以我确实希望输出类似于:那么你缺少哪一个?

array (size=2)
  0 => string 'tempId' (length=6)
  1 => string '1' (length=1)

array (size=2)
  0 => string 'tempType' (length=8)
  1 => string 'temp_first' (length=10)

array (size=2)
  0 => string 'dbId' (length=4)
  1 => string '110' (length=3)

调试代码:

foreach ($list as $row) {
  echo 'Full row:: '. $row.'<br>';

  //if the data is comming via GET or POST, its already decoded and no need to do it again
  $split = explode('&', urldecode($row));

  foreach($split as $chunk) {
    echo 'Chunk:: '.$chunk.'<br>';

    $param = explode('=', $chunk);

    echo 'param name:: '.$param[0].'<br>';
    echo 'param value:: '.$param[1].'<br>';
  }
}
于 2013-04-29T09:08:33.123 回答