【问题标题】:Store into and Display multidimensional array from mySQL using PHP使用 PHP 从 mySQL 存储和显示多维数组
【发布时间】:2017-06-01 03:50:48
【问题描述】:

我是 php 数组的新手。我想将下面的多维数组存储到来自mysql的表中,比如table_chapters其中每个数组都存储在每行中。之后,我想使用 php 以相同的格式显示来自 mysql 数据库的数据。请帮忙。

table_chapters(示例)

id |        name           | logo                |   skin
0  |    Chapter 1          | logo.1484573133.png |   chapter-1.css
1  |    Chapter 2          | null                |   chapter-2.css
2  |    Chapter 3          | null                |   chapter-3.css
3  |    Chapter 4          | null                |   chapter-4.css

数组:

$chapters = array (
  0 => 
  array (
    'name' => 'Chapter 1',
    'logo' => 'logo.1484573133.png',
    'skin' => 'chapter1.css',
    'show-time' => true,
    'streams' => 
    array (
      'Default Quality' => 
      array (
        'mp3' => 'mp3-1.mp3',
      ),
    ),
    'stats' => 
    array (
      'method' => 'server1',
      'url' => 'localhost',
      'user' => 'srem',
      'use-cover' => 'true',
    ),
  ),
  1 => 
  array (
    'name' => 'Chapter 2',
    'logo' => NULL,
    'skin' => 'chapter2.css',
    'show-time' => true,
    'streams' => 
    array (
      'Default Quality' => 
      array (
        'mp3' => 'mp3-2.mp3',
      ),
    ),
    'stats' => 
    array (
      'method' => 'server1',
      'url' => 'localhost',
      'user' => 'ecr00',
      'use-cover' => 'true',
    ),
  ),
  2 => 
  array (
    'name' => 'Chapter 3',
    'logo' => NULL,
    'skin' => 'chapter3.css',
    'show-time' => true,
    'streams' => 
    array (
      'Default Quality' => 
      array (
        'oga' => 'music.oga',
      ),
    ),
    'stats' => 
    array (
      'method' => 'server1',
      'url' => 'localhost',
      'user' => 'vand',
      'use-cover' => NULL,
    ),
  ),
  3 => 
  array (
    'name' => 'Chapter 4',
    'logo' => NULL,
    'skin' => 'chapter4.css',
    'show-time' => true,
    'streams' => 
    array (
      'Default Quality' => 
      array (
        'mp3' => 'mp3-4.mp3',
      ),
    ),
    'stats' => 
    array (
      'method' => 'server1',
      'url' => 'localhost',
      'user' => 'ooff',
      'use-cover' => 'true',
    ),
  ),
);

【问题讨论】:

    标签: php mysql arrays multidimensional-array


    【解决方案1】:

    试试这个:

    foreach($chapters as $index => $chapter)
    {
        $query = "INSERT INTO table(id, name, logo, skin) VALUES(".$index.", '".$chapter['name']."', '".$chapter['logo']."', '".$chapter['skin']."')";
    
        // fire the query on the specified database table. Each iteration add a new row in the table    
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-27
      • 2020-03-11
      • 2011-10-23
      • 2012-03-07
      • 2011-07-26
      • 2017-08-25
      • 2011-04-05
      • 1970-01-01
      • 2016-05-12
      相关资源
      最近更新 更多