doujia7094
2010-07-12 07:45 阅读 45
已采纳

通过循环创建一个多维数组!

How can i make an array like below through a loop? The text will generated from database!

 $listofimages = array(
        array(  'titre' => 'PHP',
                'texte' => 'PHP (sigle de PHP: Hypertext Preprocessor), est un langage de scripts (...)',
                'image' => './images/php.gif'
        ),
        array(  'titre' => 'MySQL',
                'texte' => 'MySQL est un système de gestion de base de données (SGDB). Selon le (...)',
                'image' => './images/mysql.gif'
        ),
        array(  'titre' => 'Apache',
                'texte' => 'Apache HTTP Server, souvent appelé Apache, est un logiciel de serveur (...)',
                'image' => './images/apache.gif'
        )
    );
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

6条回答 默认 最新

  • 已采纳
    doulin3510 doulin3510 2010-07-12 07:51

    Something like :

    $listofimages = array();
    
    foreach ( $rowset as $row ) {
    
        $listofimages[] = array(
            'titre' => $row['titre'],
            'texte' => $row['texte'],
            'image' => $row['image']
        );
    
    }
    

    ?

    点赞 评论 复制链接分享
  • douzhan8652 douzhan8652 2010-07-12 07:49
    foreach ($listofimages as $image) {
        echo '<img src="'.$image['image'].'" />';
    }
    
    点赞 评论 复制链接分享
  • duanhanglekr37902 duanhanglekr37902 2010-07-12 07:50

    do something like this,

    $result = mysql_query("SELECT * FROM table;");
    while($row = mysql_fetch_assoc($result) {
        //$row will hold all the fields's value in an array
        $mymainarray[] = $row; //hold your array into another array
    }
    
    //To display
    echo "<pre>";
    print_r($mymainarray);
    echo "</pre>";
    
    点赞 评论 复制链接分享
  • dongzu0742 dongzu0742 2010-07-12 07:51

    If you only select the fields titre, texte and image in your query, then it is just (assuming MySQL):

    $listofimages = array();
    
    $result = mysql_query("SELECT titre, texte, image FROM table");
    
    while((row = mysql_fetch_assoc($result))) {
        $listofimages[] = $row;
    }
    

    mysql_fetch_assoc() will fetch the results in an associative array.

    点赞 评论 复制链接分享
  • drwj4061 drwj4061 2010-07-12 07:51

    Let's say you have done a query and get a result identifier:

    $result = mysql_query("SELECT * FROM table;");
    

    Now you can get every row as an array by using mysql_fetch_assoc($result). Now just make a final array and add all these arrays to it:

    $final_array = array();
    while($row = mysql_fetch_assoc($result) !== false) {
        $final_array[] = $row;
    }
    
    print_r($final_array);
    
    点赞 评论 复制链接分享
  • douwei7976 douwei7976 2010-07-12 08:02
    $mult_array = array();
    
    $result = mysql_query("SELECT col1,col2,... FROM table");
    
    if(mysql_num_rows($result)>0){
      while((row = mysql_fetch_assoc($result)) {
          $mult_array[] = $row;
      }
    }
    
    // if u want to pick some specific cols from data selected from database then- 
    
    
    
    if(mysql_num_rows($result)>0){
    $temp_arr = array();
      while((row = mysql_fetch_assoc($result)) {
          $temp_arr['col1'] = $row['col1'];
          $temp_arr['col2'] = $row['col2'];
          //... so on
          $mult_array[] = $temp_arr;
      }
    }
    
    点赞 评论 复制链接分享

相关推荐