簡體   English   中英

使用PHP從MySQL存儲並顯示多維數組

[英]Store into and Display multidimensional array from mySQL using PHP

我是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',
    ),
  ),
);

嘗試這個:

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    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM