用于json字符串转换的php数组转换

时间:2011-11-09 09:00:15

标签: php json

我有以下PHP数组

Array ( 
         [subjecttable] => Array ( 
                            [0] => Array ( 
                                     [subject] => maths 
                                     [svalue] => 1 
                            ) 
                            [1] => Array ( 
                                     [subject] => english 
                                     [svalue] => 2 
                            ) 
                            [2] => Array ( 
                                     [subject] => physics 
                                     [svalue] => 3 
                            ) 
         ) 
         [Monthtable] => Array ( 
                            [0] => Array ( 
                                     [month] => Jan 
                                     [mvalue] => 1 
                            ) 
                            [1] => Array ( 
                                     [month] => Feb 
                                     [mvalue] => 2 
                            ) 
                            [2] => Array ( 
                                     [month] => Mar 
                                     [mvalue] => 3 
                            ) 
            ) 
)

我从下面给出的json字符串转换了这个

{"subjecttable": [ {"subject":"maths","svalue":"1"}, {"subject":"english","svalue":"2"}, {"subject":"physics","svalue":"3"} ], "Monthtable": [ {"month":"Jan","mvalue":"1"}, {"month":"Feb","mvalue":"2"}, {"month":"Mar","mvalue":"3"} ] }

我想将上面的php数组转换为

Array ( 
  [0] => Array ( 
            [subjecttable] => subject 
            [monthtable] => month 
            [month] => jan 
            [subject] => maths 
            [svalue] => 1 
            [mvalue] => 1 
  ) 
  [1] => Array ( 
            [subjecttable] => svalue 
            [monthtable] => mvalue 
            [month] => feb 
            [subject] => english 
            [svalue] => 2 [mvalue] => 2 
  ) 
  [2] => Array ( 
            [month] => mar 
            [subject] => physics 
            [svalue] => 3 
            [mvalue] => 3 
  ) 
)

这样我的json字符串看起来像

[ {"subjecttable":"subject","monthtable":"month","month":"jan","subject":"maths","svalue":"1","mvalue":"1"}, {"subjecttable":"svalue","monthtable":"mvalue","month":"feb","subject":"english","svalue":"2","mvalue":"2"}, {"month":"mar","subject":"physics","svalue":"3","mvalue":"3"} ]

我该怎么做?

3 个答案:

答案 0 :(得分:0)

没有冒犯,标题是神秘的,文字似乎说相反

因此,如果我理解正确,你只需要做:

$new_array = array($your_array)

然后你可以把它转换成Json格式

答案 1 :(得分:0)

我不明白。 json_encode($your_array)不应该为您提供正确的结果吗?

答案 2 :(得分:0)

当然可以使用

print json_encode($array);

json_encode()函数将php对象/数组转换为javascript等效对象。