get value from array passed in url - php

I am passing a array value in url
&settings=[{"ID":"1","visibility":"not_selected"},{"ID":"56","visibility":"not_selected"},{"ID":"57","visibility":"not_selected"}]
And getting it in php using $settings=$_REQUEST['settings'] But using
foreach(is_array($settings) as $tag => $val) {
echo $combied_final[$tag]=$val;
}
Does not seems to working in this case . I want ID and Visibility separate . How can i do that ?

To access it as an array, make:
$settings=json_decode($_REQUEST['settings'], true);
and you better use $_GET, otherwise in $_REQUEST you will have also cookies.

First use json_decode to decode the values:
$list = json_decode($_REQUEST['settings']);
foreach($list as $item) {
print $item->ID . $item->visibility;
}

Related

MYSQLI/PHP How do I have to define my query/statement to output every message that was sent in a particular chatroom? [duplicate]

for output array Instead of using json_encode, use of what.(how is output(echo) array without use of json_encode?)
i use of codeigniter.
CI_Controller:
function auto_complete(){
$hotel_search = $this->input->post('search_hotel');
echo json_encode($this->model_tour->auto_complete($hotel_search));
// if use of json_encode output is '[{"name":"salal"},{"name":"salaso"},{"name":"salasi"},{"name":"salsh"}]' if don want use of json_encode output is "Array"
}
CI_model:
function auto_complete($hotel_search)
{
$query_hotel_search = $this->db->order_by("id", "desc")->like('name', $hotel_search)->get('hotel_submits');
if($query_hotel_search->num_rows()==0){
return '0';
}else{
$data = array();
foreach ($query_hotel_search->result() as $row)
{
$data[] = array('name' => $row->name);
}
return $data;
}
}
If you just want to view the array, print_r($array) or var_dump($array) will work
I'm not sure if this is what you are looking for, but it really helps show the structure of the array/JSON:
echo "<pre>";
print_r($array);
echo "</pre>";
It will show the $array broken out and will keep the tabbing so you can see how it's nested.
If you want to view the contents of the array, use print_r or vardump
If you want to do something useful with the information, you're going to have to loop through the array
foreach($this->model_tour->auto_complete($hotel_search) as $key => $value){
//do something
}
foreach($array as $key => $value)
{
echo $key . ' = ' . $value . "\n";
}
That will give you the array as string.
You can also use print_r()
print_r($array);
or var_dump()
var_dump($array);
You have a few options at your disposal:
json_encode (which you mention yourself)
print_r or var_dump (timw4mail mentions this)
implode (however, this won't work for nested arrays, and it will only display the keys)
a combination of array_map (to format individual array elements using your own function) and implode (to combine the results)
write your own recursive function that flattens an array into a string
If you're using jQuery, any .ajax(), .get(), or .post() function will try to guess the data type being returned from the HTTP request. Set the content type of your controller to JSON:
$hotel_search = $this->input->post('search_hotel');
$this->output
->set_content_type('application/json')
->set_output( json_encode($this->model_tour->auto_complete($hotel_search)) );

Get all possible $_GET parameters from URL

I have link to a website that runs a php code with the following variable:
http://www.example.com/run.php?test=abc
There are other $_GET variables usable in the link other than test, say id and title, which I don't know about. Is it possible to get the missing variables (If there are any)? In my example case it would be the id and title variables.
$_GET is an associative array of variables passed to the current script via the URL PHP Docs
So it will hold all the url parameters. You can see this by doing:
var_dump($_GET);
Then you can do something with your $_GET parameters like so:
foreach ($_GET as $getParam => $value) {
echo $getParam . ' = ' . $value . PHP_EOL;
}
Yes...
$_GET is an array.
So $_GET[0] could be the value of test
$_GET[1] could be the value of title
So what you need to to is find out how many values are held in the $_GET array or loop through the array:
foreach ($_GET as $getParam => $value) {
echo $getParam . ' = ' . $value . PHP_EOL;
}
Also, if your do print_r($_GET); you can see how all the different entries in the array.
Yes, array_keys($_GET) will give you the keys, just iterate through them

Parsing JSON in for each with increment PHP

I'm trying to parse a json file into a for each loop. The issue is the data is nested in containers with incremented numbers, which is an issue as I can't then just grab each value in the foreach. I've spent a while trying to find a way to get this to work and I've come up empty. Any idea?
Here is the json file tidied up so you can see what I mean - http://www.jsoneditoronline.org/?url=http://ergast.com/api/f1/current/last/results.json
I am trying to get values such as [number] but I also want to get deeper values such as [Driver][code]
<?php
// get ergast json feed for next race
$url = "http://ergast.com/api/f1/current/last/results.json";
// store array in $nextRace
$json = file_get_contents($url);
$nextRace = json_decode($json, TRUE);
$a = 0;
// get array for next race
// get date and figure out how many days remaining
$nextRaceDate = $nextRace['MRData']['RaceTable']['Races']['0']['Results'][' . $a++ . '];
foreach ($nextRaceDate['data'] as $key=>$val) {
echo $val['number'];
}
?>
While decoding the json there's no need to flatten the object to an Associative array. Just use it how it is supposed to be used.
$nextRace = json_decode($json);
$nextRaceDate = $nextRace->MRData->RaceTable->Races[0]->Results;
foreach($nextRaceDate as $race){
echo 'Race number : ' . $race->number . '<br>';
echo 'Race Points : ' . $race->points. '<br>';
echo '====================' . '<br>';
}
CodePad Example
You are nearly correct with your code, you are doing it wrong when you try the $a++. Remove the $a = 0, you won't need it.
Till here you are correct
$nextRaceDate = $nextRace['MRData']['RaceTable']['Races']['0']['Results']
What you have to do next is this
$nextRaceDate = $nextRace['MRData']['RaceTable']['Races']['0']['Results'];
foreach($nextRaceDate as $key => $value){
foreach($value as $key2 => $value2)
print_r($value2);
So, in my code, you are stopping at Results, and then, you want to iterate over all the results, from 0 to X, the first foreach will do that, you have to access $value for that. So, add another foreach to iterate over all the content that $value has.
There you go, I added a print_r to show you that you are iterating over what you wanted.
The problem is how you access to the elements in the nested array.
Here a way to do it:
$mrData = json_decode($json, true)['MRData'];
foreach($nextRace['RaceTable']['Races'] as $race) {
// Here you have access to race's informations
echo $race['raceName'];
echo $race['round'];
// ...
foreach($race['Results'] as $result) {
// And here to a result
echo $result['number'];
echo $result['position'];
// ...
}
}
I don't know where come's from your object, but, if you're sure that you'll get everytime one race, the first loop could be suppressed and use the shortcut:
$race = json_decode($json, true)['MRData']['RaceTable']['Races'][0];
Your problem is that the index must be an integer because the array is non associative. Giving a string, php was looking for the key '$a++', and not the index with the value in $a.
If you need only the number of the first race, try this way
$a = 0;
$nextRaceDate = $nextRace['MRData']['RaceTable']['Races']['0']['Results'][$a];
echo "\n".$nextRaceDate['number'];
maybe you need to iterate in the 'Races' attribute
If you need all, try this way :
$nextRaceDate = $nextRace['MRData']['RaceTable']['Races'];
foreach ($nextRaceDate as $key => $val) {
foreach ($val['Results'] as $val2) {
echo "\nNUMBER " . $val2['number'];
}
}

php string manipulation received by url after installing facebook app

I get this string from URL after installing a facebook page app on several pages.
tabs_added[255408179646]=1&tabs_added[197573531148]=1&tabs_added[225556742602]=1&tabs_added[201931451540]=1&tabs_added[205657687417]=1
I am looking for a way to itterate and be able to loop and echo each numeric value within brackets [].
The values are stored in $_GET['tabs_added'], and you can access them like this:
$tabs_added = $_GET['tabs_added'];
foreach($tabs_added as $key => $value){
echo $key;
}
You should probably check if the values are present with if(isset($_GET['tabs_added'])) first.
The array should be stored in $_GET['tabs_added']. So you can try something like this to loop through it;
foreach($_GET['tabs_added'] AS $key => $value)
{
echo $key;
}
parse_str is made for parsing query strings.
parse_str('tabs_added[255408179646]=1&tabs_added[197573531148]=1', $parsed);
var_dump($parsed);
You can get an array of tabs through either the $_GET or $_POST superglobal variable, depending on whether this is a GET or POST request.
$tabs = $_GET["tabs_added"]; // or $_POST['tabs_added'];
foreach($tabs as $k => $v) {
echo $k;
}
You can also check if the param is present first to avoid an Undefined Index Notice for $_GET["tabs_added"]; and subsequent Invalid argument Warning passing NULL to the foreach loop.
isset($_GET['tabs_added']

How to return the names of $_GET variables in a url php

I want to save the name of all the $_GET variables in a url, but im not sure where to start, or finish for that matter.
For example:
if i have:
url: test.com/forums.php?topic=blog&discussion_id=12
can i use php to get the name, i.e. "topic" and "discussion_id from the $_GET variables and can i then store the values: "topic" and "discussion_id" in an array?
You can get this by calling array_keys on $_GET:
$getVars = array_keys($_GET);
If this isn't about the current URL, but just some $url string you want to extract the parameters from then:
parse_str(parse_url($url, PHP_URL_QUERY), $params);
will populate $params with:
[topic] => blog
[discussion_id] => 12
Use the following code to grab data from the URL using GET. Change it to $_POST will work for post.
<?php
foreach ( $_GET as $key => $value )
{
//other code go here
echo 'Index : ' . $key . ' & Value : ' . $value;
echo '<br/>';
}
?>
$_GET is usual php-array. you may use it in foreach loop:
foreach ($_GET as $k => $v)
echo ($k . '=' . $v);
It's an array:
print_r($_GET);
Fetch the elements as you would with any other array.

Categories