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? 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. 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"]} 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? 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 ) ) 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. 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"} 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. 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 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 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 Link to comment https://forums.phpfreaks.com/topic/217274-json-help/#findComment-1128363 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.