My json data is:
{"jsondata":[[1,7,16,29,41,45],[2,12,21,31,36,45]]}
There are 2 sets of numbers and each set has 6 different number. And the number of total sets can be more according to user's input. So, there may be 3 sets which means 18 numbers, etc. If I copy/paste that in to http://json.parser.online.fr/ , I see no error. So, json data is correct, I guess.
Second, here is my PHP file:
<?php
$input = file_get_contents('php://input');
$result=json_decode($input);
var_dump($result);
?>
That works fine too. I mean, when I run these lines, in the chrome's developer tool, I can see these:
object(stdClass)#1 (1) {
["jsondata"]=>
array(2) {
[0]=>
array(6) {
[0]=>
int(1)
[1]=>
int(7)
[2]=>
int(16)
[3]=>
int(29)
[4]=>
int(41)
[5]=>
int(45)
}
[1]=>
array(6) {
[0]=>
int(2)
[1]=>
int(12)
[2]=>
int(21)
[3]=>
int(31)
[4]=>
int(36)
[5]=>
int(45)
}
}
}
So far, I think, what I have to understand is json data is correct and php can respond it back. So, how can I reach this numbers and assing them into another variable in PHP? My actual aim is send them into the mysql. If I can get them, it is easy.
Some says use key/value pair thing but the problem is, my json data creates itself according to user's input. User clicks some numbers and then the number get appended into the json data. And it continues each of six clicks:
function createjson(){
var c=1;
var json='{"jsondata":[[';
$("#kup td").each(function(){
if($(this).html()!="" && $(this).html()!="SELECTED NUMBERS")
{
json+= parseInt($(this).html())+',';
if($(this).index()%5==0 && $(this).index()!=0) //sixth number selected
{
json=json.substring(0,json.length-1);
json+="],["
}
}
});
json=json.substring(0,json.length-3);
json+="]]}";
return json;
};
I thought I found my answer here but it didn't work either or I did somethings wrong. I am very newbie on these json stuffs, so please don't force me to change whole thing. There must be a way to reach this values. So, please save me guys :-)
You can use json_encode function with second parameter to convert JSON string into array:
$input = '{"jsondata":[[1,7,16,29,41,45],[2,12,21,31,36,45]]}';
$data = json_decode($input, TRUE);
var_dump($data['jsondata']);
In this case you should get associated array that simply for using
Related
I am looping through GET values and only getting 13 values, not matter what URL I submit. Also, it is not getting the values in sequential order...
When I loop through I only get 13 values, as well as when I use a var_dump on $_GET itself; even though there are many more values to retrieve.
Here is the URL:
website.com/Questionaire.php?SurveyName=TV%20Quiz&SurveyType=MultipleChoice&Q1=Choose%20a%20character%20off%20of%20Happy%20Days?&A1=Benny%20the%20bull&A2=The%20Fonz&A3=Jack%20Cracker&Q3=Favorite%20Friends%20character?&A1=Ross&A2=Monica&A4=Joey&A5=Rachel&A6=Chandler&A7=Phoebe&Q8=Favorite%20Nickelodeon%20show?&A1=Hey%20Arnold!&A2=Rugrats&A8=Ahhhh!%20Real%20Montsters
Here are my results:
SurveyName: TV Quiz
SurveyType: MultipleChoice
Q1: Choose a character off of Happy Days?
A1: Hey Arnold!
A2: Rugrats
A3: Jack Cracker
Q3: Favorite Friends character?
A4: Joey
A5: Rachel
A6: Chandler
A7: Phoebe
Q8: Favorite Nickelodeon show?
A8: Ahhhh! Real Montsters
As you can see the results are not in sequential order and some values are even missing.
var_dump($_GET);
foreach ($_GET as $key => $value) {
echo $key.": ".$value."<br/>\n";
}
I expect these results:
SurveyName=TV Quiz
SurveyType=MultipleChoice
Q1=Choose a character off of Happy Days?
A1=Benny the bull //<- missed
A2=The Fonz //<- missed
A3=Jack Cracker
Q3=Favorite Friends character?
A1=Ross //<- missed
A2=Monica //<- missed
A4=Joey
A5=Rachel
A6=Chandler
A7=Phoebe
Q8=Favorite Nickelodeon show?
A1=Hey Arnold!
A2=Rugrats
A8=Ahhhh! Real Montsters
You cannot have identical parameter names in your query string, otherwise the last value will overwrite the previous ones. You need to have unique answer names or you will lose data. You can imagine PHP adding the parameters to $_GET with the following pseudo-code:
foreach($param as $key=>$val) {
$_GET[$key] = $val;
}
Because of this, parameters appear in the order in which they first show up in the request. So the query string ?A=1&B=2&A=3&C=4 will have A appear first, then B, and finally C. The last value for an identical parameter is the one used, so we get the following $_GET result:
array(
'A'=>3,
'B'=>2,
'C'=>4
);
Consider adding the question identifier as a prefix for each answer. For example, instead of A1 do Q1A1 and Q2A1. This will ensure that your data is not overwritten.
I would suggest using array notation for query string parameter names so that order is maintained. Something like:
?SurveyName=TV Quiz
&SurveyType=MultipleChoice
&Q[1]=Choose a character off of Happy Days?
&A[1][1]=Benny the bull
&A[1][2]=The Fonz
&A[1][3]=Jack Cracker
&Q[3]=Favorite Friends character?
&A[3][1]=Ross
&A[3][2]=Monica
&A[3][4]=Joey
&A[3][5]=Rachel
&A[3][6]=Chandler
&A[3][7]=Phoebe
&Q[8]=Favorite Nickelodeon show?
&A[8][1]=Hey Arnold!
&A[8][2]=Rugrats
&A[8][8]=Ahhhh! Real Montsters
When you name query string parameter like that, PHP will parse them into arrays:
array(4) {
["SurveyName"]=>
string(7) "TV Quiz"
["SurveyType"]=>
string(14) "MultipleChoice"
["Q"]=>
array(3) {
[1]=>
string(37) "Choose a character off of Happy Days?"
[3]=>
string(27) "Favorite Friends character?"
[8]=>
string(26) "Favorite Nickelodeon show?"
}
["A"]=>
array(3) {
[1]=>
array(3) {
[1]=>
string(14) "Benny the bull"
[2]=>
string(8) "The Fonz"
[3]=>
string(12) "Jack Cracker"
}
[3]=>
array(6) {
[1]=>
string(4) "Ross"
[2]=>
string(6) "Monica"
[4]=>
string(4) "Joey"
[5]=>
string(6) "Rachel"
[6]=>
string(8) "Chandler"
[7]=>
string(6) "Phoebe"
}
[8]=>
array(3) {
[1]=>
string(11) "Hey Arnold!"
[2]=>
string(7) "Rugrats"
[8]=>
string(21) "Ahhhh! Real Montsters"
}
}
}
I want to fetch the value of authMarket and authscheme from the Cookie. However its position is not fixed. See the below two examples.
Cookie: TANAUTH=9f8830f6378847f726e678beaa1deea3; MultisecureUserId=00198343; authMarket=be; website=YOUROFFICE; DST=R4; authscheme=SMS;
Cookie: cuvid=cdf97045b3f041ba8de0358a7c432202; _ga=GA1.3.1409531768.1486113952; TANAUTH=cce09bb700c0c6c6c1d216ce616c23c3; MultisecureUserId=00198345; authMarket=be; website=YOUROFFICE; DST=R4; authscheme=SOFTTOKEN;
How can I do that ?
given a string
$string="Cookie: TANAUTH=9f8830f6378847f726e678beaa1deea3; MultisecureUserId=00198343; authMarket=be; website=YOUROFFICE; DST=R4; authscheme=SMS ";
you can use
$data=array_column( array_map(function($v) { return explode('=',trim($v));},explode(';',substr($string,strpos($string,':')+1))),1,0);
var_dump($data);
var_dump($data['TANAUTH'],$data['authMarket']);
ouput:
array(6) {
["TANAUTH"]=>
string(32) "9f8830f6378847f726e678beaa1deea3"
["MultisecureUserId"]=>
string(8) "00198343"
["authMarket"]=>
string(2) "be"
["website"]=>
string(10) "YOUROFFICE"
["DST"]=>
string(2) "R4"
["authscheme"]=>
string(3) "SMS"
}
string(32) "9f8830f6378847f726e678beaa1deea3"
string(2) "be"
This works fine for your two examples.
However you must keep in mind that PHP has its native $_SESSION setting and fetching mechanisms.So you need to take a look at this section of the manual and use the best method which fit the most your requirement.
I have some code to intercept a POST request, and then based on the value of another key, to unset a different key. However that part isn't working.
I know this is not the ideal method of doing this, but the script is an add-on to an existing open source platform, so I'm unable to modify existing scripts.
if($_POST['id']['txt_10'] == "Initials"){
unset($_POST['id']['id[8]']);
}else if($_POST['id']['txt_10'] == "Name"){
unset($_POST['id']['id[1]']);
}
However this code doesn't do anything and doesn't even display an error. Using a var_dump($_POST['id']); I can see the key is still set.
--
This is the var_dump of the $_POST['id'] array:
array(7) {
["txt_10"]=> string(4) "Name"
["txt_11"]=> string(0) ""
[1]=> int(72)
[4]=> int(0)
[8]=> int(170)
["txt_7"]=> string(7) "wefgweg"
[5]=> int(0)
}
unset($_POST['id'][8]) would be the correct way to adress it
I am trying to decode a series of JSON messages
The JSON messgaes are received on an adhoc basis
If the JSON messages were all exactly the same format I would be fine. However they are not.
Here is an example of two different types.
object(stdClass)#11 (1) { ["SF_MSG"]=> object(stdClass)#12 (5) { ["time"]=> string(13) "1407962618000" ["area_id"]=> string(2) "NM" ["address"]=> string(2) "2B" ["msg_type"]=> string(2) "SF" ["data"]=> string(2) "FE" } }
object(stdClass)#13 (1) { ["CA_MSG"]=> object(stdClass)#14 (5) { ["to"]=> string(4) "0360" ["time"]=> string(13) "1407962618000" ["area_id"]=> string(2) "WH" ["msg_type"]=> string(2) "CC" ["descr"]=> string(4) "2S30" } }
One has a header of SF_MSG and the other CC_MSG
My question is "How do I in PHP distinguish between the different headers so I can read them in different ways
So for example echo '$Decodedjson->SF_MSG->time; will echo the time on the first one and
echo $Decodedjson->CC_MSG->to; will echo the to variable. However I need to know whether the header is SF_MSG or a CC_MSG before performing that task.
How do I read that Header title????? ie is it CC_MSG or SF_MSG ......$Decodedjson->?????
Thanks
EDIT
if ($con->hasFrame()){
$msg=$con->readFrame();
foreach (json_decode($msg->body) as $event) {
if(isset($event['SFG_MSG'])) {
$aid=($event->SF_MSG->area_id);
}
elseif(isset($event['CA_MSG'])) {
$aid=($event->CA_MSG->area_id);
}
echo $aid;
json_decode() requires true as the second parameter in order for the resultant object to be an associative array.
foreach (json_decode($msg->body, true) as $event)
once you do this, $event will be an associative array and no longer an object, so you will need to change the rest of your code to access $event as an array instead of an object.
// this won't work
$aid=($event->SF_MSG->area_id);
// change to this
$aid = $event["SF_MSG"]["area_id"];
I cannot figure out to get these values from this array. I need to know the code and name so my application knows which one to go for but I can't get the values out of there. Can someone please help me out? It's the values in the #attributes.
I'm using PHP by the way.
Thanks
array(2) {
[0]=>
object(SimpleXMLElement)#22 (2) {
["#attributes"]=>
array(2) {
["code"]=>
string(3) "HCD"
["name"]=>
string(31) "HIGH COST DELIVERY REGION SRCHG"
}
[0]=>
string(5) "71.25"
}
[1]=>
object(SimpleXMLElement)#24 (2) {
["#attributes"]=>
array(2) {
["code"]=>
string(3) "HCD"
["name"]=>
string(31) "HIGH COST DELIVERY REGION SRCHG"
}
[0]=>
string(5) "71.25"
}
}
Using SimpleXML, you're able to access the attributes on an XML by using the elements as if it was an array ($xml->elementName['attributeName'], or using the ->attributes() method as previously given).
For example, given the following code:
$xml = new SimpleXMLElement('<root><item foo="bar"></item></root>');
If I wanted to access the foo attribute on the item element, I would access it like this:
echo $xml->item['foo'];
However, there's a catch: the value that is returned is actually an instance of SimpleXMLElement. To be able to store or use it, you will need to convert it to a primitive type:
echo (string)$xml->item['foo']; // now a string
echo (int)$xml->item['foo']; // now an integer
echo (bool)$xml->item['foo']; // now a boolean
I figured it out on my own.
$xmlResponse->AccessorialCharges->OtherAccessorialCharges[$i]['code'];