I've managed to send some parameters via POST to a .php file. When I invoke var_dump($_POST);, I get the following output:
["{"abc":"1","def":"2"}"]=>
string(0) ""
I've tried to process this many, many times but I can't seem to access the variables inside the String. json_decode doesn't even work on it. What's going on here?
Somehow you have managed to transmit your data with its value as the key and no attached value, so you need to address that in whatever code is accessing the server (Perhaps you used JSON.Stringify on the object instead of just passing the object?). Until you have fixed that, you can access your data like this:
$json = json_decode(array_keys($_POST)[0]);
print_r($json);
Output:
stdClass Object (
[abc] => 1
[def] => 2
)
Related
I'm currently developing an API using Laravel for my project, the concept:
Retrieve JSON data from MySQL.
Receive user input from the Front-end (string).
Convert both JSON and string input into an array with similar structure. The array structure here is basically ["ObjectA", "ObjectA_quantity", "ObjectB", "ObjectB_quantity", ...].
Basically, eliminate the quantity of every object of Database's Array, based on every object that User Input's Array got. For example, if the Database's Array got ["pizza", "1", "burger", "2"], and the User Input's Array got ["pizza", "1"], the output of the method is expected to be ["burger", "2"].
The method that I developed will give inconsistent and confusing output, like for some object, it works well, for other it doesn't eliminate anything and if the User Input's Array too big (> 1 object), it also doesn't eliminate anything. I really welcome different approach or anything else that will give the expected output as above. Thank you very much
Here's the source code of the method I've develop: (method's located on: else if ($transactionGetter->type == 'return'), Line 148 and so forth)
https://github.com/andre-nk23/packme-backend/blob/master/app/Http/Controllers/API/TransactionController.php
if it's a JSON you must decode the value before access
$transactionGetter=json_decode($transactionGetter);
I am making a call to an API which should return a JSON array. I receive this:
Array
(
[0] => {"msg_id":"0t5OxT1ZP9VcF45L2","_text":"I want to reserve","entities":{"intent":[{"confidence":1,"value":"make_reservation","type":"value"}]}}
)
I know it's JSON because a) the docs say that's what I should be getting and b) I ran isJson($response) and got true.
I have tried to use json_decodebut the code just dies when I do (it errs saying it's expecting a string and got an array which makes sense but if I do json_encode that would just further encode the json from what I can understand).
As I understand it, I just need a way to traverse this array and get the "value:" key inside entities: intent:. However I can't figure out how to get it or where I'm wrong.
I have tried doing:
$val = $jsonArray[0]['entitites']['intent'][0]['value'] but nothing comes out.
You are trying to decode a PHP array that has encoded values.
You should try json_decode($jsonArray[0]) instead, so that you decode the value of the first array key, as that is the actual json string.
The data you posted is a php array where the value of the first element of the array is a json string.
json_decode($response[0]);
I have a PHP script that dumps data from an API.
The dump is an array
print_r($answer);
outputs
Array ( [success] => 1 [serial] => s001 [url] => http://gooole.com )
I want to have another variable called $url that holds the value url from the array (held in $answer) in PHP.
I'm unfamiliar with this.
check out extract() it will take the keys from an array, and create variable of the same name to store them in. There are a few flags you can pass it, to determine exactly what it does with things like pre-existing variables of the same name.
EDIT: as mentioned in the comments on your question, though, $url = $answer['url']; is probably the simplest way to go.
I've been hunting around for a few hours and I still have no idea what's going on. I'm new to PHP, but fairly comfortable with simple Flash stuff.
I'm passing a JSON object from PHP into Flash AS3 using URLLoaders, etc. This is my PHP-created test JSON array:
$objJSON = array('sample' => null);
$objJSON['sample'] = "TESTING";
$objJSON['sample2'] = "TESTING2";
$objJSON = json_encode($objJSON);
I return it to flash with
echo "arrayData=$jsonArray";
When I parse that as a SINGLE object in flash, using
var tempJSON = JSON.decode(event.target.data.arrayData);
I get 'TESTING' as my output (textBox.text = tempJSON.sample; using localhost via WAMP), which is correct. Everything looks good, there's communication, the JSON library is being used right, the object is there and accessible...
BUT! When I treat it as an Array (because that's what it is) by changing the code directly above (and touching NOTHING else) to:
var tempJSON:Array = JSON.decode(event.target.data.arrayData, true);
I throw a compiler error of:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.adobe.serialization.json::JSONTokenizer/nextChar()[....\json\JSONTokenizer.as:545]
Running the swf in localhost gets me no return where I used to get string. Am I making some newbie mistake that the data suddenly becomes null when I treat it like an array?
I've checked the validity of my JSON via the output in PHP and it checks out. I've made sure I have no extra echos in the PHP class being called. I'm just stumped.
FIX'D!
Guided by the comments, I basically wasn't forming my JSON to be an array, just objects with multiple properties. The correct way to do it was:
$objArray = array(
array(
"sample1" => "Testing!",
"sample2" => "Testing2!",
),
array (
"sample1" => "Testing!",
"sample2" => "Testing2!",
)
);
$objArray = json_encode($objArray);
I believe it is because your JSON is decoding into an object and not an array. This will happen if you are using non-integer values as your array keys (ie. 'sample', 'sample2').
I'm not overly familiar with AS3, but you will likely need to cast it into an Object-like instance instead of an Array.
$objJSON = array('sample' => "TESTING", 'sample2' => "TESTING2");
echo json_encode($objJSON);
// Will output
{ "sample": "TESTING", "sample2": "TESTING2" }
This is not array notation using JSON. It is object notation.
I hope this helps!
Ok so I am stuck with this xml in PHP stuff. I have gotten pretty far considering its my first 3 hours into XML all together ever in my entire life.
I am having trouble pulling data from a XML thing that has # in the name. See below (obviously im not going to post the whole XML thing but u can see how i got there below that.
SimpleXMLElement Object
(
[#attributes] => Array
(
[date] => 2010-09
[reserved] => 6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
)
)
How i got there:
echo $this->General_functions->naked($xml->property[0]->availability->month[0]);
General_functions->naked is just a fast function to wrap and print_r around the given attribute.
My question is, HOW do i get the values inside #attributes cause no matter what i try i cant figure it out. Ive searched the web for a good 45 mins with no real answer.
Thanks in advance.
David
You need to use the attributes() method to get the results as another class. So, for example, to get the date attribute:
$myElement->attributes()->date
Also note that it's not a string, it's a SimpleXML attribute. If you want to get its actual value, you need to cast it to string explicitly:
(string)$myElement->attributes()->date
Access attributes of an element just as you would elements of an array:
(string) $xml->property[0]->availability->month[0]['date']
Edited to add the cast.