If, for some reason you need to force a single object to be an array, you can use array_values() -- this can be necessary if you have an array with only one entry, as json_encode will assign it as an object otherwise :
<?php
$object[0] = array("foo" => "bar", 12 => true);
$encoded_object = json_encode($object);
?>
output:
{"1": {"foo": "bar", "12": "true"}}
<?php $encoded = json_encode(array_values($object)); ?>
output:
[{"foo": "bar", "12": "true"}]
首先上面的代码很有作用。
下面是示例代码:
PHP
$message_ptype = array('0' => '其他消息', '1' => '七七代发推广', '2' => '系统消息', '3' => '商家后台消息');
$arr_other = array('1' => '普通消息', '2' => '欢迎消息', '3' => '定一定消息', '4' => '买单消息', '5' => '推广消息', '6' => '路由器推广消息', '11' => '商家学堂消息');//其他消息
$arr_qiqi_gen = array('1' => '新开通买单','2'=>'新开通储值券','3'=>'到店提醒','4'=>'欢迎语','5'=>'节日问候','6'=>'周末问候');//七七代发推广
$arr_sys_msg = array('1'=>'分享领券','2'=>'抽奖领券','3'=>'设备异常','4'=>'发现可用WIFI','5'=>'外送付款成功','6'=>'付款成功通知');//系统消息
$arr_bus_admin = array('1'=>'收款成功通知');//商家后台消息
if($op == 'get_ptype'){
if(preg_match('/[^\d]/', $ms_ptype)) die('reject');
if(is_array($message_ptype) && count($message_ptype) > 0){
if($ms_ptype == 0) {
$m_type_return = sel_type($arr_other);//其他
}else if($ms_ptype == 1){
$m_type_return = sel_type($arr_qiqi_gen);//七七代发推广
}else if($ms_ptype == 2){
$m_type_return = sel_type($arr_sys_msg);//系统消息
}else if($ms_ptype == 3){
$m_type_return = sel_type($arr_bus_admin);//商家后台消息
}
}
die(json_encode($m_type_return));
}
function sel_type($arr_s){
$arr_return = array();
foreach($arr_s as $key => $val){
$arr_return[$key]['ms_name'] = $val;
$arr_return[$key]['ms_type'] = $key;
}
return array_values($arr_return);
}
HTML页面:
<tr>
<td>发送时间</td>
<td>
<input id="ms_send_start" name="ms_send_start" type="text" value="<{$food.ms_send_start}>">---
<input id="ms_send_end" name="ms_send_end" type="text" value="<{$food.ms_send_end}>">
</td>
<td>消息父类型</td>
<td>
<select name="ms_ptype" id="ms_ptype" οnchange="get_ptype()">
<{foreach from=$message_ptype item=item key=key}>
<option value="<{$key}>" <{if $key==$food.ms_ptype && isset($food.ms_ptype)}>selected<{/if}>><{$item}> </option>
<{/foreach}>
</select>
</td>
<td>消息子类型</td>
<td><select name="ms_type" id="ms_type">
<option value="-1">全部</option>
<{foreach from=$ms_type_arr item=item key=key}> <option value="<{$key}>" <{if $key==$food.ms_type && isset($food.ms_type)}>selected<{/if}>><{$item}>
</option>
<{/foreach}>
</select></td>
<td width="37%"><input type="button" οnclick="change_val()" name="msg_text" value="测试消息查询"></td>
</tr>
JS代码:
$ga_ajax = null
function get_ptype(){
$("#ms_type").html('');
$ms_ptype = $("#ms_ptype").val();
if($ga_ajax != null) $ga_ajax.abort();
$ga_ajax = $.ajax({
url:'index.php?m=<{$MOD}>&n=<{$MODS}>&t=' + (new Date()).getTime(),
type:'post',
data:{'ms_ptype':$ms_ptype,'op':'get_ptype'},
dataType:'text',
success:function(data){
try{
if(data != 'reject' && data != 'error'){
$json = $.parseJSON(data);
draw_ms_ptype($json);
}
}catch(e){
alert('系统繁忙,请稍候再试...');
}
}
});
}
function draw_ms_ptype($data){
for($i=0; $i < $data.length; $i++){
$('<option value="' + $data[$i].ms_type + '">' + $data[$i].ms_name + '</option>').appendTo($("#ms_type"));
}
}
总结:上面提到了有很重要的几点总结一下
1.JS代码中
$("#ms_type").html('');
切换时清空原有数据,否则会无限叠加
2.PHP代码中
array_values($arr_return);
这是将对象转为数组。不这样做的话,会达不到效果