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" }]