PHPでデータベースデータをJSON形式に変換する方法。たとえば、「phone」:「123456」を「name」:「phone」,「value」:「123456」に変更します
「phone」を変更する方法:」 1234567」から「名前」:「電話番号」、「値」:「123456」。 。
------ソリューションのアイデア----------------------
$s = '{"phone":"1234567"}';<br /><br />$a = json_decode($s, 1);<br />foreach($a as $k=>$v) $r[] = array('name' => $k, 'value' => $v);<br /><br />echo json_encode($r);<br />
<br />$a = '{"phone":"1234567"}';<br />$ret = array();<br />$arr = json_decode($a, true); <br />foreach($arr as $key=>$val){<br /> array_push($ret, array('name'=>$key,'value'=>$val));<br />}<br /><br />echo json_encode($ret);<br />
<br />$s="'phone':'1234567'";<br />$arr=explode(':',$s);<br />for($i=0;$i<sizeof($arr);$i++){<br /> $item=array('name'=>current($arr),'value'=>next($arr));<br /> break;<br />}<br />echo json_encode($item);<br />
<br />{"name":"'phone'","value":"'1234567'"}<br />
$s = '[{"phone":"18511337033","name":"ggg","id_card":"123321123321"}]';<br />$a = json_decode($s, 1);<br /><br />foreach($a as $t) {<br /> $r = array();<br /> foreach($t as $k=>$v) $r[] = array('name' => $k, 'value' => $v);<br /> $res[] = $r;<br />}<br /><br />echo json_encode($res);<br />
<br />$s = '[{"phone":"18511337033","name":"ggg","id_card":"123321123321"}]';<br />$a = json_decode($s, 1);<br /> <br />foreach($a as $t) {<br /> $r = array();<br /> foreach($t as $k=>$v) $r[] = array('name' => $k, 'value' => $v);<br /> $res[] = $r;<br />}<br /> <br />echo json_encode(array_pop($res));<br />