I have a curl function which outputs the following text from an api.
number=1&id=731&name=test&value=6311
How could I make a variable which will store one of these values in the text such as I want to make a variable called $name which will have the 'test' value from the output of the api. I saw different things such as in json you can use $variable['name'] but it doesn't seem to work like this.. help
Check this:
Parse query string into an array
Use parse_str function, it looks like:
$get_string = "number=1&id=731&name=test&value=6311";
parse_str($get_string, $get_array);
print_r($get_array);
Related
PHP is unable to get the value for dynamic object prepared as:
$abc->{$dynamic_object_pattern}
Where the value of the variable $dynamic_object_pattern is, json->{'data_1'}->{'value'}
For me, PHP 7.1 is understanding the statically defined pattern like below, and fetching the value as it should:
$abc->json->{'data_1'}->{'value'}
But not when I put the whole portion into a variable and then try to get its value. I Tried,
$abc->{$dynamic_object_pattern} and $abc->$dynamic_object_pattern
both ways, but no solution yet.
The error comes is Notice: Undefined property: stdClass::$json->{'data_1'}->{'value'}
I'm attempting an answer without seeing your JSON data
Here you say :
But not when I put the whole portion into a variable and then try to
get its value
From that line alone it sounds like you are trying to get value from a string rather than array. If you put the whole portion into a variable, PHP will interpret it as string. Make sure you add array() before newly created variable.
Natural array :
$array = array();
Now a string
$variable = $array;
Convert string to array
$new_array = array($variable);
Also, have you tried decoding?
// decode
$response = json_decode($new_array, true);
//print out
var_export(array_unique(array($response)));
I want something like this:
I have send a request to server which contains my_par=test.
I can get it like this $my_par= $_REQUEST["my_par"]. this is the normal way.
But I want if my variable name is match to the request name then it take the value of that parameter automatically.
so I want a function like set_variable_value(array($my_par,$my_par2),$_REQUEST), then it should set the value for variables like this:
$my_par = $_REQUEST["my_par"];
$my_par2 = $_REQUEST["my_par2"];
is it possible to achieve such a thing in PHP?
OR in a simple way
if I send a request like this my_par1=test&my_par2=test2, I want to access them just by add $ at the beginning of their name like this:
$my_par1 and $my_par2
Extract the $_POST array, like this:
extract($_POST);
This will give you variables named after $_POST's keys, with corresponding values.
I have no idea why you'd want to do this, but this can be achieved by using parse_str():
$query = http_build_query($_REQUEST);
parse_str($query);
So, if the request was like below:
test.php?foo=test1&bar=test2&baz=test3
... you can simply access the variables using their query parameter names, like so:
echo $foo;
would output:
test1
With the above code, you may accidentally override already defined variables. If you use the second parameter of parse_str(), you can store the variables in an array, instead:
parse_str($query, $params);
That way, you don't set/override variables in the current scope.
I have an associative array after json_encode like this
{"1":"CourseA", "2":"CourseB"}
and it is stored in a php variable named $jsonObject.Now, I want to send this to a javascript function and use that array inside that function. The
function is invoked onclick like this:
link
The problem is: It shows an error:invalid id popup({....
Whats the reason and what should be the solution for that? btw, I am working in moodle and the above link is shown inside a moodle block and declared inside $this->content->text.
Html encode the JSON to escape special characters,
link
I would like to display the text 'hamsters repeat one'.
My array:
a:1:{i:0;s:19:"hamsters repeat one";}
Should I use var_dump for this? In PHP. I know how to get the array data out of the database (I am using get_post_meta a wordpress feature for this) .
Should it be something like this? :
var_dump $variablewhicharrayhasbeenstored;
Thank-you
Use unserialize($myvar) to decode that string into an array.
Can I get return via URL in php like below?
$var = http://www.example.com/give_data.php?id=4;
Your question is very confusing, but i think you want the contents of the file in your variable?
For that you can use file_get_contents
$var = file_get_contents('http://php.net/manual/en/function.file-get-contents.php');
You can use file_get_contents() as such:
$var = file_get_contents('http://www.xyz.com/give_data.php?id=4');
But that will just return whatever the give_data.php script outputs as a string. If you need more advanced data-types, I would recommend using json_encode() in give_data.php and $var = json_decode($var); after getting the data to get back your original data. Note that this won't work for complex PHP objects. If you really really need, you can use serialize() and unserialize(), but it's not as standard and will work in PHP-only.