将多维数组转换为数据库

时间:2012-10-10 19:13:43

标签: php mysql database arrays

我正在尝试用PHP编写一个巨大的配置数组,看起来像这样

$config['festival'] = 
array
(
    'title' => 'USF Tango Festival',
    'tableLayout' => array
    (
        'registration' => array
        (
            array('firstName','text'),
            array('lastName','text'),
            array('phone','text'),
            array('email','text'),
            array('hearAboutFestival','text'),
            array('danceAs','enum(\'Leader\', \'Follower\', \'Both\')'),
            array('student','tinyint(1)')
        ),
        'experience' => array
        (
            array('options','text'),
            array('lunchMeat','enum(\'Ham\',\'Turkey\',\'Vegetarian\')'),
            array('lunchBread','enum(\'White\',\'Wheat\')'),
            array('dinnerPref','enum(\'Chicken\',\'Beef\',\'Vegetarian\')')
        )
    ),
    'pageLayout' => array
    (
        'registration' => array
        (
            'jqueryRules' =>
<<<EOT
    'firstName': 'required',
    'lastName': 'required',
    'phone': {
        required: true,
        phoneUS: true
    },
    'email': {
        required: true,
        email: true
    },
    'danceAs': 'required',
    'partner': 'required',
    'partnerMatching': {
        required: function() {
            return $("input[name='partner']").val() == 0;
        }
    },
    'partnerName': {
        required: function() {
            return $("input[name='partner']").val() == 1;
        }
    }
EOT
    ,
        'inputs' => array
        (
            array
            (
                'type' => 'text',
                'name' => 'firstName',
                'fullName' => 'First Name',
                'required' => true,
                'separateDiv' => false
            ),
            array
            (
                'type' => 'text',
                'name' => 'lastName',
                'fullName' => 'Last Name',
                'required' => true,
                'separateDiv' => false
            ),
            array
            (
                'type' => 'text',
                'name' => 'phone',
                'fullName' => 'Phone number',
                'required' => true,
                'separateDiv' => false
            ),
            array
            (
                'type' => 'text',
                'name' => 'email',
                'fullName' => 'Email address',
                'required' => true,
                'separateDiv' => false
            ),
            array
            (
                'type' => 'text',               
                'name' => 'hearAboutFestival',
                'separateDiv' => false,
                'fullName' => 'How did you hear about the festival?',
                'required' => false
            ),
            array
            (
                'type' => 'select',
                'name' => 'danceAs',
                'fullName' => 'You dance as a...',
                'required' => true,
                'separateDiv' => false,
                'options' => array(array('leader','Leader'),array('follower','Follower'),array('both','Both'))
            ),
            array
            (
                'type' => 'checkbox',
                'name' => 'student',
                'value' => '1',
                'separateDiv' => false,
                'fullName' => 'I am a student',
                'required' => false
            )
        )
    )

'options' => array
(
    'busMilonga' => array
    (
        'price' => 20,
        'student' => false,
        'name' => 'Tango on the Town Bus milonga',
        'description' => 'A bus milonga!'
    ),
    'thursdayMilonga' => array
    (
        'price' => 10,
        'student' => false,
        'name' => 'Thursday Kickoff Milonga',
        'description' => 'The un-official kick off milonga!'
    ),
    'saturdayPass' => array
    (
        'price' => 90,
        'student' => true,
        'name' => 'Saturday pass',
        'description' => 'Includes all Saturday workshops and milongas'
    ),
    'sundayPass' => array
    (
        'price' => 80,
        'student' => true,
        'name' => 'Sunday pass',
        'description' => 'Includes all Sunday workshops, the jam session, and milonga'
    ),
    'milongaPass' => array
    (
        'price' => 70,
        'earlyBird' => 50, //array(50,60),
        'student' => true,
        'name' => 'Milonga Pass',
        'description' => 'Includes entrance to all night milongas'
    ),
    'dinnerPass' => array
    (
        'price' => 20,
        'student' => false,
        'name' => 'Dinner pass',
        'description' => 'Includes Saturday dinner'
    ),
    'lunchPass' => array
    (
        'price' => 10,
        'student' => false,
        'name' => 'Saturday lunch',
        'description' => 'Includes lunch on Saturday'
    )
),
'info' => array
(
    'instructors' => array('Instructors'),
    'hour' => array('10','11','12','01','02','03'),
    'min' => array('15','30','45','00'),
    'tod' => array('AM','PM'),
    'day' => array('Friday','Saturday','Sunday'),
    'level' => array('Beginner','Intermediate','Advanced'),
    'place' => array('REC 107','REC 033','REC 005'),
    'sessions' => array
    (
        3, // days
        array
        (
            'Friday', // day name
            3, // sessions on this day
            array
            (
                3, // workshops in session
                '1100AM' // time
            ), 
            array
            (
                3, 
                '0100PM' 
            ), 
            array
            (
                1,
                '0230PM'
            ) // 1,4
        ),
        array('Saturday', // 2,0
            3, // 2,1
            array(1,'1030AM'), //2,2
            array(3,'1145AM'), // 2,3
            array(3,'0145PM') // 2,4
        ),
        array
        (
            'Sunday', // 3,0
            3, // 3,1
            array(1,'1115AM'), // 3,2
            array(3,'1230PM'), // 3,3
            array(3,'0230PM') // 3,4
        )
    )
)

进入数据库。我想我可以制作一些表格,标题类似于config.event.festival,但之后它会变得很麻烦,因为我必须为数组下的每个数组创建一个表...

我想避免使用JSON编码或序列化,这样我保持数据的所有关系和干净的外观,但我不知道除了保留一个大的配置文件而不是数据库之外的任何其他方式。

2 个答案:

答案 0 :(得分:2)

假设您提供的示例代表整个数据结构的截断视图,可以使用三个表来完成。

  • festival_id
  • 标题

布局

  • layout_id
  • festival_id
  • layout_type_id
  • 姓名
  • 全名
  • 需要
  • separateDiv

layout_type

  • layout_type_id
  • 名称

节日表将保留有关活动页面的高级元数据。布局表将包含有关页面上每个元素的元数据。最后,layout_type表将允许您识别应该在给定页面上的不同元素类型。

这应该让你开始并允许你根据需要进行修改。

答案 1 :(得分:1)

试试这个:

// To save multidimensional array into database:

$confIn = serialize($config);

// Save serialized config into database

// To get it from database, query the database and get serialized value

$confOut = serialize($confIn);

// Check if its ok

var_export($confOut);

有关序列化功能的更多信息:Serialize

这种方法的好处是你只能在数据库中使用一列。

希望这有帮助!