-1

我正在尝试在 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 编码或序列化,这样我就可以保持数据的所有相关性和干净的外观,但除了保留一个大的配置文件而不是数据库之外,我不知道任何其他方式。

4

2 回答 2

2

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

节日

  • 节日ID
  • 标题

布局

  • layout_id
  • 节日ID
  • layout_type_id
  • 类型
  • 姓名
  • 全名
  • 必需的
  • 单独的Div

布局类型

  • layout_type_id
  • 姓名

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

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

于 2012-10-10T19:28:12.643 回答
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);

更多关于序列化函数:序列化

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

希望这可以帮助!

于 2012-10-10T19:26:20.123 回答