php_guest Posted June 21, 2009 Share Posted June 21, 2009 I am wondering what I am doing wrong in the following code, because I get error: Warning: Illegal offset type in isset or empty in D:\xampp\htdocs\testing\4.php on line 5 $_POST[firstkey][secondkey]=12; if (isset($_POST)) { foreach($_POST as $key){ foreach ($_POST[$key] as $key2 => $value){ echo "<br />Key=".$key2."value=".$value; } } } How would be the correct was echo in the same statement 1st and 2nd key and value? Thank you Quote Link to comment Share on other sites More sharing options...
joel24 Posted June 21, 2009 Share Posted June 21, 2009 i don't think you can set an $_POST value manually?? i.e. $_POST[firstkey][secondkey]=12; won't work? what if $_POST array isn't multidimensional? i'd do if (isset($_POST)) { foreach($_POST as $key){ if (is_array($_POST[$key])) { foreach ($key as $key2 => $value){ echo "<br />Key=".$key2."value=".$value; } } } oh, and what code is on line 5? is it isset($_POST)? and the second foreach should be foreach $key as $key2 etc etc i think you're problem was you were trying to use an array as an index.. i.e. $_POST[$key] when $key was an array. use that foreach $key as $key2 and you should be fine Quote Link to comment Share on other sites More sharing options...
php_guest Posted June 21, 2009 Author Share Posted June 21, 2009 I can not set it manually because I get it from previous page where names of inputs are set automatically. Quote Link to comment Share on other sites More sharing options...
joel24 Posted June 21, 2009 Share Posted June 21, 2009 I can not set it manually because I get it from previous page where names of inputs are set automatically. ... so you have a form on the previous page and its being posted to the page with the script you posted? what are you trying to achieve by your foreach loops?? if you just want to look at the contents of the $_POST array you can use print_r($_POST); have you tried this code? it *should* work if (isset($_POST)) { foreach($_POST as $key){ if (is_array($_POST[$key])) { foreach ($key as $key2 => $value){ echo "<br />Key=".$key2."value=".$value; } } } Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 21, 2009 Share Posted June 21, 2009 joel24 was referring to this line - $_POST[firstkey][secondkey]=12; And yes, you can set it manually. $_POST is an array afterall. Nothing really special about that. But people really need to learn to use better practices. When coding use error_reporting(E_ALL);. There is a PHP notice for using undefined constants as keys. php_guest - joel24's code should work. If not, please explain your problem more clearly. Quote Link to comment Share on other sites More sharing options...
joel24 Posted June 21, 2009 Share Posted June 21, 2009 And yes, you can set it manually. $_POST is an array afterall. Nothing really special about that. But people really need to learn to use better practices. When coding use error_reporting(E_ALL);. There is a PHP notice for using undefined constants as keys. interesting! just had a play around you can even set $_SERVER['PHP_SELF'] etc etc.. always assumed they'd be locked... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.