How PHP turns database data into JSON format, such as "Phone": "123456" to "name": "Phone", "value": "123456"

Source: Internet
Author: User
How to change "Phone": "1234567" to "name": "Phone", "value": "123456".


Reply to discussion (solution)

$s = ' {' phone ': ' 1234567 '} '; $a = Json_decode ($s, 1); foreach ($a as $k + = $v) $r [] = Array (' name ' = = $k, ' value ' = = $v ); Echo Json_encode ($r);
[{"Name": "Phone", "value": "1234567"}]

$a = ' {' phone ': ' 1234567 '} '; $ret = Array (); $arr = Json_decode ($a, true); foreach ($arr as $key = + $val) {    Array_push ($ret, Array (' name ' = = $key, ' value ' = = $val));} echo Json_encode ($ret);


[{"Name": "Phone", "value": "1234567"}]

$s = "' phone ': ' 1234567 '", $arr =explode (': ', $s); for ($i =0; $i current
 
  
   
  ($arr), ' Value ' =>next ($arr));    break;} echo Json_encode ($item);
 
  

{"Name": "' Phone '", "value": "' 1234567 '"}

$s = ' {' phone ': ' 1234567 '} '; $a = Json_decode ($s, 1); foreach ($a as $k + = $v) $r [] = Array (' name ' = = $k, ' value ' = = $v ); Echo Json_encode ($r);
[{"Name": "Phone", "value": "1234567"}]


[{"Phone": "18511337033", "name": "GGG", "Id_card": "123321123321"}]
$sql = "SELECT * from E_user where phone= ' 18511337033 '"; $result = Get_js_array ($sql); $a = Json_decode ($result); foreach ($a as $k = = $v) $r [] = Array (' name ' = = $k, ' value ' = = $v); echo Json_encode ($r); Echo $r;


That's how I rewrite it. But when I checked it out, it became like this.
[{"Name": 0, "value": {"Phone": "18511337033", "name": "GGG", "Id_card": "123321123321}}] Array

My Get_js_array () is written like this.
function Get_js_array ($sql) {$result = mysql_query ($sql); $arr = Array (); while ($row = Mysql_fetch_array ($result, Mysql_ ASSOC) {$arr [] = $row;  } $js = Json_encode ($arr); Mysql_free_result ($result); Mysql_close ($conn); return $js;}


How to Modify

$s = ' [{' Phone ': ' 18511337033 ', ' name ': ' GGG ', ' id_card ': ' 123321123321 '}] ', $a = Json_decode ($s, 1); foreach ($a as $t) {  $r = Array ();  foreach ($t as $k = + $v) $r [] = Array (' name ' = = $k, ' value ' = = $v);  $res [] = $r;} echo Json_encode ($res);
[[{"Name": "Phone", "value": "18511337033"},{"name": "Name", "Value": "GGG"},{"name": "Id_card", "Value": "123321123321 "}]]

$s = ' [{' Phone ': ' 18511337033 ', ' name ': ' GGG ', ' id_card ': ' 123321123321 '}] ', $a = Json_decode ($s, 1); foreach ($a as $t) {  $r = Array ();  foreach ($t as $k = + $v) $r [] = Array (' name ' = = $k, ' value ' = = $v);  $res [] = $r;} echo Json_encode ($res);
[[{"Name": "Phone", "value": "18511337033"},{"name": "Name", "Value": "GGG"},{"name": "Id_card", "Value": "123321123321 "}]]



。。 Now there's a need. That's how you do it. not two [[{' Name ': ' 111 ', ' value ': ' 222 '}]],
Output only one square bracket [{"Name": "111", "Value": "222"}]


$s = ' [{' Phone ': ' 18511337033 ', ' name ': ' GGG ', ' id_card ': ' 123321123321 '}] ', $a = Json_decode ($s, 1); foreach ($a as $t) {  $r = Array ();  foreach ($t as $k = + $v) $r [] = Array (' name ' = = $k, ' value ' = = $v);  $res [] = $r;} echo Json_encode ($res);
[[{"Name": "Phone", "value": "18511337033"},{"name": "Name", "Value": "GGG"},{"name": "Id_card", "Value": "123321123321 "}]]



。。 Now there's a need. That's how you do it. not two [[{' Name ': ' 111 ', ' value ': ' 222 '}]],
Output only one square bracket [{"Name": "111", "Value": "222"}]



Change?? :

$s = ' [{' Phone ': ' 18511337033 ', ' name ': ' GGG ', ' id_card ': ' 123321123321 '}] '; $a = Json_decode ($s, 1); foreach ($a as $t) {  $r = array ();  foreach ($t as $k = + $v) $r [] = Array (' name ' = = $k, ' value ' = = $v);  $res [] = $r;} Echo Json_encode (Array_pop ($res));


[{"Name": "Phone", "value": "18511337033"},{"name": "Name", "Value": "GGG"},{"name": "Id_card", "Value": "123321123321" }]
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.