Questions about inserting 3D arrays into the database

Source: Internet
Author: User
Questions about inserting a 3D array into a database $ arrDataarray (manager & gt; 5, blogname & gt; blog, domain & gt; bokee, user [1] & gt; array (uid & gt; 1, name & gt; zhangsan), user [2] Questions about database insertion of 3D arrays
$ ArrData = array (
'Manager' => '5 ',
'Blogname' => 'blog ',
'Domain '=> 'bokee ',
'User [1] '=> array ('uid' => 1, 'name' => 'hangsan '),
'User [2] '=> array ('uid' => 2, 'name' => 'lisi '),
);
In this array, 'manager', 'blogname', and 'domain 'are inserted into one table, and user [] is inserted into another table. user [] may be 3, 4, 5 ..... how can this problem be solved? Thank you.

------ Solution --------------------
PHP code
$ ArrData = array ('manager' => '5', 'blogname' => 'blog ', 'domain' => 'bokee ', 'user [1] '=> array ('uid' => 1, 'name' => 'hangsan '), 'user [2] '=> array ('uid' => 2, 'name' => 'lisi'),); foreach ($ arrData as $ value) {if (gettype ($ value) === 'array') {echo $ value ['uid']; echo $ value ['name'] ;}}
------ Solution --------------------
Programmers have control over the program.
------ Solution --------------------
It is a two-dimensional array:
PHP code
$ ArrData = array ('manager' => '5', 'blogname' => 'blog ', 'domain' => 'bokee ', 'user [1] '=> array ('uid' => 1, 'name' => 'hangsan '), 'user [2] '=> array ('uid' => 2, 'name' => 'lisi '),); # This is a two-dimensional array foreach ($ arrData as $ k => $ data) {if (is_array ($ data) $ user [] = $ data; else $ arr [$ k] = $ data;} print_r ($ arr); # Array ([manager] => 5 [blogname] => blog [domain] => bokee) print_r ($ user); # Array ([0] => Array ([uid] => 1 [name] => zhangsan) [1] => Array ([uid] => 2 [name] => lisi ))
------ Solution --------------------
Write an insert method and a field generation method.
Split two arrays first
An error may occur when you try it on your own.
PHP code
$ ArrData = array ('manager' => '5', 'blogname' => 'blog ', 'domain' => 'bokee ', 'user [1] '=> array ('uid' => 1, 'name' => 'hangsan '), 'user [2] '=> array ('uid' => 2, 'name' => 'lisi '),); $ data1 = array ('manager' => '5', 'blogname' => 'blog ', 'domain' => 'bokee',); $ data2 = (array) $ arrData ['user']; // we will not talk about 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 ;}// generated result: uid, name} function create ($ key, $ value, $ table) {insert into $ table ($ key) values ($ value )}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.