三维数组插入数据库有关问题

WBOY
Release: 2016-06-13 13:13:29
Original
732 people have browsed it

三维数组插入数据库问题
$arrData = array(
'manager' =>'5',
'blogname' => '博客',
'domain' => 'bokee',
'user[1]'=>array('uid' => 1,'name'=>'zhangsan'),
'user[2]'=>array('uid'=>2,'name' => 'lisi'),
);
这个数组中的'manager','blogname','domain'要插到一个表中,而user[]要插到另一个表中,user[]还可能3,4,5.....请问这样要怎么做?谢谢了

------解决方案--------------------

PHP code

$arrData = array(
'manager' =>'5',
'blogname' => '博客',
'domain' => 'bokee',
'user[1]'=>array('uid' => 1,'name'=>'zhangsan'),
'user[2]'=>array('uid'=>2,'name' => 'lisi'),
);
foreach($arrData as $value){
    if(gettype($value) === 'array'){
        echo $value['uid'];
        echo $value['name'];
    }
}
<br><font color="#e78608">------解决方案--------------------</font><br>程序员对程序有百分百的控制权啊.
<br><font color="#e78608">------解决方案--------------------</font><br>楼主,那是二维数组:<br>
Copy after login
PHP code

$arrData = array(
'manager' =>'5',
'blogname' => '博客',
'domain' => 'bokee',
'user[1]'=>array('uid' => 1,'name'=>'zhangsan'),
'user[2]'=>array('uid'=>2,'name' => 'lisi'),
);

#这是二维数组
foreach($arrData as $k=>$data){
    if(is_array($data))
        $user[] = $data;
    else
        $arr[$k] = $data;
}
print_r($arr);
#Array ( [manager] => 5 [blogname] => 博客 [domain] => bokee )
print_r($user);
#Array ( [0] => Array ( [uid] => 1 [name] => zhangsan ) [1] => Array ( [uid] => 2 [name] => lisi ) )
<br><font color="#e78608">------解决方案--------------------</font><br>这个就写1个 插入方法 和 字段生成方法吧.<br>先把 2个数组拆分<br>随手打的 可能有错误, 自己调整下。<br>
Copy after login
PHP code

$arrData = array(
'manager' =>'5',
'blogname' => '博客',
'domain' => 'bokee',
'user[1]'=>array('uid' => 1,'name'=>'zhangsan'),
'user[2]'=>array('uid'=>2,'name' => 'lisi'),
);
$data1 =array('manager' =>'5','blogname' => '博客','domain' => 'bokee',);
$data2 =(array)$arrData['user'];

//data1 我们就不说了.
function add($data,$table)
{
   foreach($data as $v){
   $key = implode('`,`',array_keys($v));
   $key = '`'.$key.'`';
   $value = implode("','",array_values($v));
   $value = "'".$value ."'";
   if(!$res = create($key,$value,$table))
   {
        return $res;
   }
   }
   //生成结果:uid,name
}
function create($key,$value,$table){
    insert into $table($key) values($value)
} <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template