Jump to content

JSON help


steviez

Recommended Posts

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
Share on other sites

 

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.