steviez Posted October 30, 2010 Share Posted October 30, 2010 Hi, I have two select boxes on my site like this: Language - Level and the user can keep adding more sets of select boxes. I need the data posted to be JSON encoded like this: {"LANG1":["German","Beginner"],"LANG2":["Polish","Advanced"]} etc How can i make this happen? Quote Link to comment https://forums.phpfreaks.com/topic/217274-json-help/ Share on other sites More sharing options...
trq Posted October 30, 2010 Share Posted October 30, 2010 json_encode. Quote Link to comment https://forums.phpfreaks.com/topic/217274-json-help/#findComment-1128291 Share on other sites More sharing options...
steviez Posted October 30, 2010 Author Share Posted October 30, 2010 json_encode. Sorry i should have said that i am already using this function but cant get the result i need. My code is this: if(isset($_POST)) { foreach($_POST as $key => $val) { echo json_encode(array($key => array($val['lang'], $val['level']))); } } but it does not output the required: {"LANG1":["German","Beginner"],"LANG2":["Polish","Advanced"]} Quote Link to comment https://forums.phpfreaks.com/topic/217274-json-help/#findComment-1128292 Share on other sites More sharing options...
trq Posted October 30, 2010 Share Posted October 30, 2010 What does.... echo '<pre>'; print_r($_POST); echo '</pre>'; look like? Quote Link to comment https://forums.phpfreaks.com/topic/217274-json-help/#findComment-1128300 Share on other sites More sharing options...
steviez Posted October 30, 2010 Author Share Posted October 30, 2010 What does.... echo '<pre>'; print_r($_POST); echo '</pre>'; look like? well at the moment with 4 of each field selected i get: Array ( [lang] => Array ( [0] => german [1] => polish [2] => english [3] => blaaaa ) [level] => Array ( [0] => 2 [1] => 4 [2] => 1 [3] => 3 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/217274-json-help/#findComment-1128301 Share on other sites More sharing options...
trq Posted October 30, 2010 Share Posted October 30, 2010 All you need do is.... echo json_encode($_POST); then. The array is already how you need it. Quote Link to comment https://forums.phpfreaks.com/topic/217274-json-help/#findComment-1128303 Share on other sites More sharing options...
steviez Posted October 30, 2010 Author Share Posted October 30, 2010 All you need do is.... echo json_encode($_POST); then. The array is already how you need it. that gives me: {"lang":["german","polish","english","blaaaa"],"level":["2","4","1","3"]} and i need: {"german":"2","polish":"4", "english":"1","blaaaa":"3"} Quote Link to comment https://forums.phpfreaks.com/topic/217274-json-help/#findComment-1128305 Share on other sites More sharing options...
trq Posted October 30, 2010 Share Posted October 30, 2010 That is not at all what your original post described. Quote Link to comment https://forums.phpfreaks.com/topic/217274-json-help/#findComment-1128309 Share on other sites More sharing options...
steviez Posted October 30, 2010 Author Share Posted October 30, 2010 That is not at all what your original post described. yes it is... except i wanted the results as: {"LANG1":["German","Beginner"],"LANG2":["Polish","Advanced"]} I changed it as i thought it might get more complicated if not Quote Link to comment https://forums.phpfreaks.com/topic/217274-json-help/#findComment-1128310 Share on other sites More sharing options...
kenrbnsn Posted October 30, 2010 Share Posted October 30, 2010 Try something like this (I initialize the $_POST array in this code for testing only): <?php $_POST = Array ( 'lang' => Array ( 'german', 'polish', 'english', 'blaaaa' ), 'level' => Array ( 2,4,1,3 ) ); $tmp = array(); $i=1; foreach($_POST['lang'] as $ind => $lang) { $tmp['lang'.$i] = array($lang,$_POST['level'][$ind]); $i++; } $js = json_encode($tmp); echo $js . "<br>\n"; ?> The output is {"lang1":["german",2],"lang2":["polish",4],"lang3":["english",1],"lang4":["blaaaa",3]} Ken Quote Link to comment https://forums.phpfreaks.com/topic/217274-json-help/#findComment-1128313 Share on other sites More sharing options...
steviez Posted October 30, 2010 Author Share Posted October 30, 2010 Try something like this (I initialize the $_POST array in this code for testing only): <?php $_POST = Array ( 'lang' => Array ( 'german', 'polish', 'english', 'blaaaa' ), 'level' => Array ( 2,4,1,3 ) ); $tmp = array(); $i=1; foreach($_POST['lang'] as $ind => $lang) { $tmp['lang'.$i] = array($lang,$_POST['level'][$ind]); $i++; } $js = json_encode($tmp); echo $js . "<br>\n"; ?> The output is {"lang1":["german",2],"lang2":["polish",4],"lang3":["english",1],"lang4":["blaaaa",3]} Ken You sir are a life saver! thank you so much Quote Link to comment https://forums.phpfreaks.com/topic/217274-json-help/#findComment-1128363 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.