Reputation: 11
Since I'm using a foreach loop, all the parameters are shown. I have several parameters which I want to include from my URL, but I want to remove the last parameter and it's value from automatically showing. The code is as follows:
<?php
$query = explode('&', $_SERVER['QUERY_STRING']);
$params = array();
foreach( $query as $param )
{
list($name, $value) = explode('=', $param, 2);
echo $params[urldecode($name)][] = urldecode($value);
echo "<br/>";
}
echo "<br/> Total amount placed: ".$total = $_GET["total"];
?>
The URL is this one:
confirmation.php?betSlip=Juve&betSlip=Milan&total=0.43
As you can see the url consists of several items inside a betSlip parameter, and it consists the total of all items. I want to remove the total from displaying in the foreach loop and be able to display it only whenever I call it.
Thanks
Upvotes: 1
Views: 854
Reputation: 11
DavidDomain is right. You could alternatively unset just that specific key value pair in the array, if you can't guarantee it'll always be the last array element.
$params = $_GET;
unset($params['total']);
// $params is now array('betSlip' => 'Juve', 'betSlip' => 'Milan');
Upvotes: 1
Reputation: 15293
Just use array_pop
to remove the last element of the $query
array. Given that total
will always be the last param.
$params = "betSlip=Juve&betSlip=Milan&total=0.43";
$query = explode('&', $params);
array_pop($query); // remove the last element of $query
$params = array();
foreach( $query as $param )
{
list($name, $value) = explode('=', $param, 2);
echo $params[urldecode($name)][] = urldecode($value);
echo "<br/>";
}
echo "<br/> Total amount placed: ".$total = $_GET["total"];
Upvotes: 0