Hej
Czy ktoś może mi pomóc dlaczego w wyniku json_encode dosateje
"zmienna_1": "true",
"zmienna_1": true,
Skoro spodziewasz się, że zmienna będzie miała wartość boolean, to zrób po prostu:
No właśnie nie wiem tego, bo wysyłam setki zmiennych POST, jedne są bool, inne tekst a inne liczbowe. Nie wiem jak to ogarnąć.
Zrobiłem coś takiego
if ($input == "true" || $input == "false") { (bool)$input; } if (http://www.php.net/is_numeric($input)) { http://www.php.net/settype($input, "integer"); }
A co niby dzieje się w linii 2?
Dobra mój błą
Tak miało być
if (http://www.php.net/is_numeric($input)) { http://www.php.net/settype($input, "integer"); } if ($input === "true" || $input === "false") { $input = (bool)$input; }
$config_file = http://www.php.net/file_get_contents($file); $decoded_data = json_decode($config_file, true); foreach ($_POST as $POST_key => $POST_value) { if ($POST_key != "submit" && $POST_key != "ID") { $POST_key_right = http://www.php.net/substr($POST_key, http://www.php.net/strpos($POST_key, "=") + 1); $POST_key_left = http://www.php.net/strtok($POST_key, '='); if (!http://www.php.net/is_array($POST_value)) { if ($POST_key_left == "Hunt_") { foreach($decoded_data as $Primary_Key => $Primary_Val) { foreach($Primary_Val as $Secondary_Key => $Secondary_Val ) { if ($Secondary_Key === "Hunt_") { foreach($Secondary_Val as $Third_Key => $Third_Val ) { if ($Third_Key == $POST_key_right) { if ($POST_value == "true") { $decoded_data[$Primary_Key][$Secondary_Key][$Third_Key] = true; } else if ($POST_value == "false") { $decoded_data[$Primary_Key][$Secondary_Key][$Third_Key] = false; } } } } } } } else { foreach($decoded_data as $Primary_Key => $Primary_Val) { foreach($Primary_Val as $Secondary_Key => $Secondary_Val ) { if ($Secondary_Key == $POST_key) { if (http://www.php.net/is_numeric($POST_value)) { http://www.php.net/settype($POST_value, "integer"); $decoded_data[$Primary_Key][$Secondary_Key] = $POST_value; } else if ($POST_value == "true") { $decoded_data[$Primary_Key][$Secondary_Key] = true; } else if ($POST_value == "false") { $decoded_data[$Primary_Key][$Secondary_Key] = false; } else { $decoded_data[$Primary_Key][$Secondary_Key] = $POST_value; } } } } } } if (http://www.php.net/is_array($POST_value)) { http://www.php.net/echo "TABLICA - "; http://www.php.net/echo $POST_key_left, " - ", $POST_value, "<BR>"; } } $enc_data = json_encode($decoded_data, JSON_PRETTY_PRINT, JSON_NUMERIC_CHECK); file_put_contents($file, $enc_data);
Odpowiadając na pierwszy post, skoro używasz json_encode, to przy
[php]$foo = ['a'=>1, 'b'=>true, 'c'=>false, 'd' => new stdClass, 'e'=>'stop'];
$json = json_encode($foo);
print($json);
var_dump(json_decode($json, true));/php]
to wynikiem są:
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)