从另一个数组结果创建一个数组 - php

时间:2015-06-12 09:57:17

标签: php mysql arrays count sum

我有两种课:数学和物理。 在循环中,count(lessonclasses)等于2然后是3。 如何进行预期的输出,

我的代码是:

  $result[]=array();
  foreach($lessons as $nameLesson)
{
$lessonclasses=$Board->GetLessonsClass($iduser,$nameLesson);
$tabPrice=$redevance->detailPriceForLesson($nameLesson)
$price=$tabPrice[0];
$frequency=$tabPrice[1];
//echo "nb of class per lesson: ".count($lessonclasses);
for($i=0;$i<count($lessonclasses);$i++)
    { 
    //var_dump($lessonclasses);
    $name=$lessonclasses[$i][0]; 
    $amount=$lessonclasses[$i][1];
    $timelessonstart=$lessonclasses[$i][2]; 
    $timelessonend=$lessonclasses[$i][3]; 

    $result[$i][]=array($name,$price."€/".$frequency,$timelessonstart,$timelessonend,$amount);
    //$arrayOptions[$nbLessons][]=$options; //for keep values
    //$nbLessons=$nbLessons+1;
    }
    }   
      var_dump($result);

我有一个像:

这样的数组
array(3) {
 [0]=> array(2) { 
    [0]=> array(5) { 
        [0]=> string(15) "maths" 
        [1]=> string(11) "10.00" 
        [2]=> string(10) "2015-06-17" 
        [3]=> string(1) "-"
         [4]=> string(6) "10.00" 
    }
     [1]=> array(5) { 
        [0]=> string(31) "physique" 
        [1]=> string(10) "6.00" 
        [2]=> string(10) "2015-01-01" 
        [3]=> string(1) "-"
         [4]=> string(5) "36.00"
     }
     }
 [1]=> array(2) {
         [0]=> array(5) {
         [0]=> string(15) "maths" 
        [1]=> string(11) "20.00" 
        [2]=> string(10) "2015-06-17"
         [3]=> string(1) "-" 
        [4]=> string(6) "100.0" 
          } 
[1]=> array(5) {
         [0]=> string(31) "physique" 
        [1]=> string(10) "16.00"
         [2]=> string(10) "2015-05-01" 
        [3]=> string(1) "-"
         [4]=> string(5) "36.00" 
         }
 }
 [2]=> array(1) {
         [0]=> array(5) {
            [0]=> string(15) "Maths" [1]=> string(11) "11.00" [2]=> string(10) "2015-05-17" [3]=> string(1) "-" [4]=> string(5) "20.00" } } } 

我的预期输出是:

array(3) { 
[0]=> array(2) { 
    [0]=> array(5) { 
        [0]=> string(15) "maths" 
        [1]=> string(11) "10.00" 
        [2]=> string(10) "2015-06-17" 
        [3]=> string(1) "-"
         [4]=> string(6) "10.00" 
    }
     [1]=> array(2) {
         [0]=> array(5) {
         [0]=> string(15) "maths" 
        [1]=> string(11) "20.00" 
        [2]=> string(10) "2015-06-17"
         [3]=> string(1) "-" 
        [4]=> string(6) "100.0" 
          } 
    [2]=> array(5) {
         [0]=> string(31) "math" 
        [1]=> string(10) "11.00"
         [2]=> string(10) "2015-05-01" 
        [3]=> string(1) "-"
         [4]=> string(5) "36.00" 
     }
 }
 [1]=> array(2) { 
    [0]=> array(5) { 
        [0]=> string(31) "physique" 
        [1]=> string(10) "6.00" 
        [2]=> string(10) "2015-01-01" 
        [3]=> string(1) "-"
         [4]=> string(5) "36.00"

     }

    [1]=> array(5) {
         [0]=> string(31) "physique" 
        [1]=> string(10) "16.00"
         [2]=> string(10) "2015-05-01" 
        [3]=> string(1) "-"
         [4]=> string(5) "36.00" 
     }

 }

}}

4 个答案:

答案 0 :(得分:0)

您需要使用sum

SELECT name,sum(qty) FROM table_name group by name;

答案 1 :(得分:0)

SELECT name,sum(qty) FROM tableName group by name;

答案 2 :(得分:0)

简单地

SELECT name, SUM(qty) AS total_amount FROM table GROUP BY name

答案 3 :(得分:0)

它为你工作..

SELECT name,sum(qty) FROM table_name GROUP BY name;