php json_encode 格式輸出 數組轉 json
public function json_echo1 ($code = 0, $msg = '',$result = array(),$have_img = 0)
{
if ($have_img)
{
$this->_add_http_imgage($result);
}
$arr = array (
"code" => $code ,
"msg" => $msg ,
"result" => $result
);
echo json_encode($arr);
exit();
}
public function _add_http_imgage (&$array)
{
if (is_array($array))
{
foreach ($array as $k => $v)
{
$this->_add_http_imgage($array[$k]);
}
}else{
$abc = substr($array,- 4);
if (strncasecmp($abc,".jpg",4) == 0 || strncasecmp($abc,".png",4) == 0 || strncasecmp($abc,".bmp",4) == 0 || strncasecmp($abc,".gif",4) == 0)
{
if (strncasecmp(substr($array,0,4),"http",4) == 0)
{
}else{
$array = 'http://'.$_SERVER['HTTP_HOST']."/" . $array;
}
}
}
}
調用示例
return $this->json_echo1(1,'成功',$result,1);