Jump to content

JSON help


steviez

Recommended Posts

 

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.