PHP - How to get $_GET Parameter without value - php

I need to know how to get the $_GET parameter from requested url. If I use for example
$url = $_SERVER['REQUEST_URI'];
$parse_url = parse_url($url, PHP_URL_QUERY);
echo $parse_url; will output query=1, but I need only the parameter not parameter AND value to check if parameter is in array.

Based on your example, simply exploding the = might quickly suits your need.
$url = $_SERVER['REQUEST_URI'];
$parse_url = parse_url($url, PHP_URL_QUERY);
$queryparam = explode("=", $parse_url);
echo $queryparam[0];
/* OUTPUT query */
if (in_array($queryparam[0], $array_of_params)){ ... }
But you can simply achieve the same thing like this:
if (#$_GET["query"] != ""){ ... }

Something like:
// Example URL: http://somewhere.org?id=42&name=bob
// An array to store the parameters
$params = array();
parse_str($_SERVER['QUERY_STRING'], $params);
// Access what you need like this:
echo 'ID: ' .$params['id'];

The Arry $_GET contains all needed informations. $ _SERVER ['REQUEST_URI'] and parse_url not needed.
As an example I have the following browser line:
http://localhost/php/test.php?par&query=1
Let's see what $ _GET contains
echo '<pre>';
var_export($_GET);
Output
array (
'par' => '',
'query' => '1',
)
With array_keys() I get all keys.
$keys = array_keys($_GET);
var_export($keys);
Output:
array (
0 => 'par',
1 => 'query',
)
The first key I get with (see also comment from #bobble bubble)
echo $keys[0]; //par
or
echo key($_GET);
With array_key_exists () I can check whether a key is available.
if(array_key_exists('query',$_GET)){
echo 'query exists';
}

You can use basename() too, it Returns trailing name component of path. For example:
$lastPart=explode("=",basename('http://yourdomain.com/path/query=1'));
echo $lastPart[0];
Output
query

Related

How to convert into json object like array

Mycode is given below:
// seatsToCancel=U23,L43,U12
$tin='S4243534'; //Booking id
$seatsToCancel=$_GET['seatsToCancel'];
$seatArray=explode(",",$seatsToCancel);
$seatCount=count($seatArray);
$SeatsTocancel=implode ( '", "', $seatArray );
$params = array(
"tin"=>"$tin",
"seatsToCancel"=>array($SeatsTocancel)
);
echo $params=json_encode($params);
I want output like this:
{"tin":"S4243534" ,"seatsToCancel":["U23","L43","U12"]}
I assume your $seatArray is ["U23","L43","U12"]?
In that case you can do like this:
$seatArray = ["U23","L43","U12"];
$finalarray = ["tin"=>$tin,"seatsToCancel" => $seatArray];
echo json_encode($finalarray); //{"tin":"S4243534","seatsToCancel":["U23","L43","U12"]}
Edited to add the new requirement from edited question
So I'm guessing that your seats to cancel are coming in as a comma-separated list from the URL. So all you need to do is explode() the GET parameter to turn it into an array, place it as the value to a key and json_encode() it.
i.e.
$seatsToCancel = explode(',', $_GET['seatsToCancel']);
$params = [
'tin' => $tin,
'seatsToCancel' => $seatsToCancel,
];
$paramsJson = json_encode($params);
That should get you the result you're after:
{"tin":"S4243534" ,"seatsToCancel":["U23","L43","U12"]}
You may want to consider what happens if the GET parameter is empty and whether or not you want any additional error checking.

How to get all parameter's value from given URL?

I have one URL string like
'http://example.com/glamour/url.php?ad_id=[ad_id]&pubid=[pubid]&click_id=[click_id]'
So I want to fetch value of all parameters which mentioned in [].
Like ad_id,pubid,clickid
How can I fetch values of parameters?
<?php
echo $_GET['ad_id']; // ad_id is the name of parameter.
?>
This code will give you the value of parameter
For String of URL try this :
$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['ad_id'];
You could use this command.
print_r($_GET);
And then you could see the all parameters in the url.
Use this simple code to get all parts of query:
<?php
/* Parse url */
$url = 'http://example.com/url.php?ad_id=[ad_id]&pubid=[pubid]';
parse_str( parse_url( $url, PHP_URL_QUERY ), $query_data );
/* Results */
var_dump($query_data['ad_id']);
var_dump($query_data['pubid']);
?>

Echo array_values from array

I am very new in PHP and I was checking whether all my controlers are in so how can I echo this? what I tried resulting nothing
$controllers = array_intersect($json_api->get_controllers(), $active_controllers );
$return = array(
'json_api_version' => $version,
'controllers' => array_values($controllers)
);
echo $return['controllers']['controllers'];
use print_r function:
print_r($return['controllers']);
when you want read city you do:
$arr_controllers = $return['controllers'];
$key_2 = $arr_controllers[2];
where 2 is the key
If you want to print the values of an array with a
understandable format you should use print_r() instead of echo like so:
print_r($return['controllers']);
You can also use var_dump() to get some extra information about the fields, like the type and lenght.
If what you need is to asign a certain index of the array to a value just do something like this:
$variable = $return['controllers'][indexOfField]; // indexOfField=2 for city field
echo $variable;
For further information about print_r() check the official manual.

PHP Build url query from array

I'm trying to build URL query from an Array that looks like that:
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
I would like to get query like that:
localhost/get?serial=3804689&serial=3801239&serial=3555689
(you get the idea)
I'm trying to use http_build_query($serials, 'serial', '&'); but it adds the numeric index to the prefix 'serial'.
Any idea how to remove that numeric index?
Well you can't really have the same GET parameter in the string, After all if you try to access that variable server side, what would you use?
$_GET['serial'] - But which serial would it get?
If you really want to get a list of serials, Simply turn the array into a string, save it as an array and there you go. for example :
$serials = "string of serials, delimited by &";
Then you can use the http build query.
Maybe use a foreach:
$get = "localhost/get?serial=" . $serials[0];
unset( $serials[0] );
foreach( $serials AS serial ){
$get .= "&serial=$serial;
}
Just as an FYI, PHP doesn't handle multiple GET variables with the same name natively. You will have to implement something fairly custom. If you are wanting to create a query string with multiple serial numbers, use a delimiter like _ or -.
Ex: soemthing.com/serials.php?serials=09830-20990-91234-12342
To do something like this from an array would be simple
$get_uri = "?serial=" . implode("-", $serials);
You would be able to get the array back from a the string using an explode to
$serials = explode("-", $_GET['serials']);
Yes its quite possible to have such format, you have to build it query string by indices. Like this:
No need to build the query string by hand, use http_build_query() in this case:
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$temp = $serials;
unset($serials);
$serials['serial'] = $temp;
$query_string = http_build_query($serials);
echo urldecode($query_string);
// serial[0]=3804689&serial[1]=3801239&serial[2]=3555689&serial[3]=3804687&serial[4]=1404689&serial[5]=6804689&serial[6]=8844689&serial[7]=4104689&serial[8]=2704689&serial[9]=4604689
And then finally, if you need to process it somewhere, just access it thru $_GET['serial'];
$serials = $_GET['serial']; // this will now hold an array of serials
You can also try this
$get = "localhost/get?serial=";
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$list = implode("&serial=",$serials);
echo $get.$list;
Having the same GET parameter will not work this way. You will need to use an array in the get parameter:
localhost/get?serial[]=3804689&serial[]=3801239&serial[]=3555689
IF you just print $_GET through this URL, you will receive
Array ( [serial] => Array ( [0] => 380468 [1] => 3801239 [2]=> 3555689) )
Then you can do this to build your query string
$query_str = 'serial[]='.implode('&serials[]=',$serials);
You can try this version.
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$get = implode('&serials[]=',$serials);
echo 'test';
var_dump($_GET['serials']);
Result

getting variables from STRING url in php

If I have a url that looks like this, what's the best way to read the value
http://www.domain.com/compute?value=2838
I tried parse_url() but it gives me value=2838 not 2838
Edit: please note I'm talking about a string, not an actual url. I have the url stored in a string.
You can use parse_url and then parse_str on the query.
<?php
$url = "http://www.domain.com/compute?value=2838";
$query = parse_url($url, PHP_URL_QUERY);
$vars = array();
parse_str($query, $vars);
print_r($vars);
?>
Prints:
Array
(
[value] => 2838
)
For http://www.domain.com/compute?value=2838 you would use $_GET['value'] to return 2838
$uri = parse_url($uri);
parse_str($uri['query'], $params = array());
Be careful if you use parse_str() without a second parameter. This may overwrite variables in your script!
parse_str($url) will give you $value = 2838.
See http://php.net/manual/en/function.parse-str.php
You should use GET method e.g
echo "value = ".$_GET['value'];

Categories