您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php foreach有关问题

2024/3/31 13:55:37发布20次查看
php foreach问题,在线等
pc客户端post如下json数据至后台,json result数组中的数据条目不固定,有时是1组,有时是3组,可能更多。
如:json result 数组中1组数据
{
   result : [
      {
         old_ip : 61.141.251.21,
         new_ip : 61.141.251.22,
         urldata : img1.taobao.com\r,
         normal : 100,
         error : 102,
         agent : mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1; qqdownload 732; .net4.0c; .net4.0e)\r
      }
   ]
}
如:json result 数组中3组数据
{
   result : [
      {
 old_ip : 61.141.251.23,
         new_ip : 61.141.251.24,
         urldata : img1.taobao.com\r,
         normal : 100,
         error : 102,
         agent : mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1; qqdownload 732; .net4.0c; .net4.0e)\r
      },
      {
 old_ip : 61.141.251.25,
         new_ip : 61.141.251.26,
         urldata : img2.taobao.com\r,
         normal : 100,
         error : 102,
         agent : mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1; qqdownload 732; .net4.0c; .net4.0e)\r
      },
      {
 old_ip : 61.141.251.27,
         new_ip : 61.141.251.28,
         urldata : img3.taobao.com\r,
         normal : 100,
         error : 102,
         agent : mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1; qqdownload 732; .net4.0c; .net4.0e)\r
      }
   ]
}
数据表:
create table `result` (
  `id` int(10) unsigned not null auto_increment,
  `old_ip` varchar(100) not null,
  `new_ip` varchar(100) not null,
  `urldata` varchar(100) not null,
  `normal` varchar(10) not null,
  `error` varchar(10) not null,
  `agent` varchar(100) not null,
  primary key  (`id`)
) engine=myisam default charset=utf8 auto_increment=1 ;
后台接收php代码:
请教各位专家,如何使用foreach取出$arr中的数据,并写入数据库(需考虑result数据条目不固定的问题),格式如下,求完整代码!合适可加分!
id old_ip           new_ip         urldata          normal  error  agent
1  61.141.251.21    61.141.251.22  img1.taobao.com  100     102    mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1; qqdownload 732; .net4.0c; .net4.0e)
2  61.141.251.23    61.141.251.24  img2.taobao.com  100     102    mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1; qqdownload 732; .net4.0c; .net4.0e)
------解决思路----------------------
$user_ip=get_userip();
$result=$_post[result];
$arr=json_decode($result,true);
$arr=$arr['result'];
if($arr){
foreach($arr as $val){
$val['user_ip'] = $user_ip;
$val = array_map('mysql_real_escape_string',$val);
$str = '.implode(',', $val).';
$sqlstr = insert into result(old_ip,new_ip,urldata,normal,error,agent,user_ip) values(.$str.);
echo $sqlstr.'
';
mysql_query($sqlstr) or die(mysql_error());
}
}

该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product