Hack Forums
[PHP] Put variable into array without quotes - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Put variable into array without quotes (/showthread.php?tid=5792440)



Put variable into array without quotes - deadsh0t - Jan 30, 2018

So i am stuck with one problem i have $amount wich is $_post["amount"] however the rpc requires that numeric values has to be inserted without quotes so when i type the amount manually in the php pages it works and generates this request:

"{"method":"sendTransaction","params":{"anonymity":0,"fee":1000000,"addresses":["S6EHUg5k8K2br3mAVnd13AGGjYqXw1rCzjGn6z7Wnf7eaAE8nZtwZD1dcc2L3h137P1NTKxh9HM6y5AfBHtwnGjp21jW52w1q"],"transfers":[{"amount":70000,"address":"S6F8bN49qSUK2SunXpB8rnEp6LdCXoS2f6ZSfC8pxmhYjhqqkNj6o4jdcc2L3h137P1NTKxh9HM6y5AfBHtwnGjp21jVCmZDm"}]}}"

But when i use the value from the form it generates this request

"{"method":"sendTransaction","params":{"anonymity":0,"fee":1000000,"addresses":["S6EHUg5k8K2br3mAVnd13AGGjYqXw1rCzjGn6z7Wnf7eaAE8nZtwZD1dcc2L3h137P1NTKxh9HM6y5AfBHtwnGjp21jW52w1q"],"transfers":[{"amount":"70000","address":"S6F8bN49qSUK2SunXpB8rnEp6LdCXoS2f6ZSfC8pxmhYjhqqkNj6o4jdcc2L3h137P1NTKxh9HM6y5AfBHtwnGjp21jVCmZDm"}]}}"

here is the code:
PHP Code

<?php 
$da 
= array("method" => "sendTransaction", "params" => array("anonymity" => 0 ,"fee" => 1000000, "addresses" => array($address1), "transfers" => array(array("amount" => $amount  ,"address" => $receive)))); 



RE: Put variable into array without quotes - Devsoft™ - Jan 30, 2018

(Jan 30, 2018 03:15 AM)deadsh0t Wrote:  So i am stuck with one problem i have $amount wich is $_post["amount"] however the rpc requires that numeric values has to be inserted without quotes so when i type the amount manually in the php pages it works and generates this request:

"{"method":"sendTransaction","params":{"anonymity":0,"fee":1000000,"addresses":["S6EHUg5k8K2br3mAVnd13AGGjYqXw1rCzjGn6z7Wnf7eaAE8nZtwZD1dcc2L3h137P1NTKxh9HM6y5AfBHtwnGjp21jW52w1q"],"transfers":[{"amount":70000,"address":"S6F8bN49qSUK2SunXpB8rnEp6LdCXoS2f6ZSfC8pxmhYjhqqkNj6o4jdcc2L3h137P1NTKxh9HM6y5AfBHtwnGjp21jVCmZDm"}]}}"

But when i use the value from the form it generates this request

"{"method":"sendTransaction","params":{"anonymity":0,"fee":1000000,"addresses":["S6EHUg5k8K2br3mAVnd13AGGjYqXw1rCzjGn6z7Wnf7eaAE8nZtwZD1dcc2L3h137P1NTKxh9HM6y5AfBHtwnGjp21jW52w1q"],"transfers":[{"amount":"70000","address":"S6F8bN49qSUK2SunXpB8rnEp6LdCXoS2f6ZSfC8pxmhYjhqqkNj6o4jdcc2L3h137P1NTKxh9HM6y5AfBHtwnGjp21jVCmZDm"}]}}"

here is the code:
PHP Code

<?php 
$da 
= array("method" => "sendTransaction", "params" => array("anonymity" => 0 ,"fee" => 1000000, "addresses" => array($address1), "transfers" => array(array("amount" => $amount  ,"address" => $receive)))); 

I am not sure if this will work, but you can try using function stripslashes()

PHP Code

<?php 
$da 
= array("method" => "sendTransaction", "params" => array("anonymity" => 0 ,"fee" => 1000000, "addresses" => array($address1), "transfers" => array(array("amount" => stripslashes($amount), "address" => $receive)))); 



RE: Put variable into array without quotes - deadsh0t - Jan 30, 2018

(Jan 30, 2018 06:57 AM)WingTsun™ Wrote:  I am not sure if this will work, but you can try using function stripslashes()

PHP Code

<?php 
$da 
= array("method" => "sendTransaction", "params" => array("anonymity" => 0 ,"fee" => 1000000, "addresses" => array($address1), "transfers" => array(array("amount" => stripslashes($amount), "address" => $receive)))); 

thanks however it doesnt seem to work , but i just fixed it by adding JSON_NUMERIC_CHECK to de encoding part


RE: Put variable into array without quotes - Vyxn - Jan 30, 2018

Have you tried using this, real_escape_string.


RE: Put variable into array without quotes - Charger - Jan 30, 2018

PHP Code

<?php 
$s 
= "abcdefg";
$n1 = "100.99";
$n2 = 100.325;

$x = array("S" => $s, "n1" => $n1, "n2" => $n2);

echo 
json_encode($x);

echo 
"<br>";

$x = array("S" => $s, "n1" => (float) $n1, "n2" => $n2);
echo 
json_encode($x); 

returns
{"S":"abcdefg","n1":"100.99","n2":100.325}

{"S":"abcdefg","n1":100.99,"n2":100.325}


You need to type cast/juggle -> http://php.net/manual/en/language.types.type-juggling.php


RE: Put variable into array without quotes - Exzib - Jan 30, 2018

Try using intval, seems like $amount is a string.

Replace $amount with intval($amount).


RE: Put variable into array without quotes - deadsh0t - Jan 31, 2018

(Jan 30, 2018 09:39 AM)Charger Wrote:  
PHP Code

<?php 
$s 
= "abcdefg";
$n1 = "100.99";
$n2 = 100.325;

$x = array("S" => $s, "n1" => $n1, "n2" => $n2);

echo 
json_encode($x);

echo 
"<br>";

$x = array("S" => $s, "n1" => (float) $n1, "n2" => $n2);
echo 
json_encode($x); 

returns
{"S":"abcdefg","n1":"100.99","n2":100.325}

{"S":"abcdefg","n1":100.99,"n2":100.325}


You need to type cast/juggle -> http://php.net/manual/en/language.types.type-juggling.php

yes this works also i will keep this in mind thanks for now im using JSON_NUMERIC_CHECK


RE: Put variable into array without quotes - Charger - Feb 5, 2018

(Jan 31, 2018 02:43 AM)deadsh0t Wrote:  yes this works also i will keep this in mind thanks for now im using JSON_NUMERIC_CHECK

Glad that fixed it. Just keep in mind that JSON_NUMERIC_CHECK is kind of a half-assed constant in PHP.
http://www.garageenbois.fr/posts/why-json_numeric_check-is-not-a-good-idea