php如何在数组中复制值(多维)

时间:2016-06-06 03:05:09

标签: php mysql

我的数据库表是:

author               book                    repeat
------               ----                    ------
Paulo Coelho         the alchemist           2
Adam Smith           The Wealth of Nations   1 

我使用以下php代码从db获取数据:

...
$books_array[]= array(
       'author'=> $row['author'],
       'book'=> $row['book'],
);

但我不知道如何根据我的重复值重复这些值。我如何获得以下数组?

 array
     [0] => Array
        (
          [author] => Coelho
          [book] => The Alchemist
        )

     [1] => Array
        (
          [author] => Coelho
          [book] => The Alchemist
        )

     [2] => Array
        (
          [author] => Smith
          [book] => The Wealth of Nations
        )

谢谢!

1 个答案:

答案 0 :(得分:0)

Oups!想想!那很简单!

刚刚添加

 for( $i= 0 ; $i <= $row['repeat'] ; $i++ ){
   $books_array[]= array(
   'author'=> $row['author'],
   'book'=> $row['book'],
    );
 };

希望这对其他人有帮助:) !!!!