How to unset a key on a $_POST array? - php

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

Related

How to fetch a string from a line if the position of the string is not fixed | php

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.

PHP: Incorrect value after assignment

I have really no idea what's happening. The story:
I use PDO for a SELECT statement on a database.
$sql = "SELECT a,b,c,performance,points,compare
FROM normvalues
WHERE x=:x AND y=1 AND z=:z";
$stmt = $GLOBALS['PDO']->prepare($sql);
$stmt->bindParam(":x",$x);
$stmt->bindParam(":z",$z);
$stmt->execute();
$res=$stmt->fetchAll(PDO::FETCH_ASSOC);
So that's fine and it is working. When I var_dump the $res variable I get something like:
array(6) {
["a"]=> string(2) "44"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "50.1"
["points"]=> string(1) "1"
["compare"]=> string(2) "-1"
}
[1]=>
array(6) {
["a"]=> string(2) "57"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "47.7"
["points"]=> string(1) "2"
["compare"]=> string(2) "-1"
}
[2]=>
array(6) {
["a"]=> string(2) "70"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "44.7"
["points"]=> string(1) "3"
["compare"]=> string(2) "-1"
}
...
That's also okay. But I have to sort the result another way. So I am doing:
foreach($res as $e){
$this->normTable[$e['a']][$e['points']]=$e['performance'];
$this->normTable[$e['a']]['compare']=$e['compare'];
}
And now I am completely lost. By assigning $e['performance'] I get wrong values. Actually this should be the performance values.
[1176]=>
array(4) {
[1]=> string(2) "50"
["compare"]=> string(2) "-1"
[2]=> string(2) "48"
[3]=> string(2) "45"
}
I already checked the values in the database and they are correct. By doing doubleval() I'd get the right values, but the problem is that not every value is a double but also integer or string. I also tried to typecast with (string) but it's the same result. I have no explanation.
Update:
It's a very big project and I just tried to minimize it as possible and to make my problem as clear as possible. But now I have figured out something new:
I do an 'echo()' of my first variable in the normTable during the loop:
foreach($res as $e){
$this->normTable[$e['a']][$e['points']]=$e['performance'];
echo "a:".$e['a']." pt: ".$e['points']." perf: ".$e['performance']."-".$this->normTable[1176][1]."\n";
$this->normTable[$e['a']]['compare']=$e['compare'];
}
and the value is changing from '50.1' to '50'. Still can't figure out the reason. Is there a size limitaion of arrays in PHP?
UPDATE 2 and a big SORRY!
As I said, it is a big project. So the table I read out, has some values for some attributes twice or more. Actually such a case should not happen. That's why the answer is simple: It became 50 because 50 was assigned. I'm so sorry for having waisted your time. But I totally excluded this case and since I am also coding in C, my first thought was: memory leak - clear case!
Thanks for your help.
Meh, I don't have 50 reputation; can't comment only answer.
If you replace a with be in your loop, you should get your expected result (given your data sample). I.e.:
foreach($res as $e){
$this->normTable[$e['b']][$e['points']]=$e['performance'];
$this->normTable[$e['b']]['compare']=$e['compare'];
}
But I'm not sure this really solves your problem. YMMV.

json_decode in PHP with multiple of the same names but different values

I am trying to decode a json data set with many entires that have the same names but are paired with multiple different values.
Example:
{"id":"XXX",
"price":"XXX",
"size":"XXX",
"id":"YYY",
"price":"YYY",
"size":"YYY"}
I have no problem building an array when there is only one value for each name by doing this:
$array = json_decode($array4,true);
PHP array:
array(3) {
["id"]=> string(3) "XXX"
["price"]=> string(3) "XXX"
["size"]=> string(3) "XXX"
}
but when I try to use the whole file which contents ~4000 different values with paired between ~4 names I get a response of NULL when I want the json_decode to be something like:
array(3) {
["id"]=> string(3) ("XXX","YYY")
["price"]=> string(3) ("XXX","YYY")
["size"]=> string(3) ("XXX","YYY")
}
Can anyone help?
Update: I needed to built the json input correctly.
Like below:
{"data":[
{"id":"XXX","price":"XXX","size":"XXX"},
{"id":"YYY","price":"YYY","size":"YYY"}]
}
Then json_decode works great.

How to get a specific array value from an XML File?

I have written an xml-parser to read an XML-file. The XML-file is not mine so I can't change the structure. Things work great till I got to this special point. I want to read a value but I don't have a key to access this value.
I marked the values (in red) in the screenshot below which I want to access.
When I dump the parent element (the PRAT->VALUE) I get this in return:
object(SimpleXMLElement)#31 (3) { ["#attributes"]=> array(5) { ["nr"]=> string(1) "1" ["unit"]=> string(3) "bar" ["unit_id"]=> string(4) "3103" ["vo"]=> string(0) "" ["vo_id"]=> string(0) "" } [0]=> string(2) "20" [1]=> string(1) "2" }
As seen, at the end of the dump the values that I want to access are presented. I tried to access it like an array but that doesn't work. The values are not part of the attributes.
use (String) keyword in front of it.
eg.
echo (String) PRAT->VALUE;

handling json data with php

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

Categories