Distribute array to other array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nurul02
    New Member
    • Apr 2010
    • 7

    Distribute array to other array

    Hi,

    I really need help here,and already running out of any idea.
    I have this situation whereby I need to distribute lists of lecturers to all students.
    Eg:

    Num of students : 10
    Num of lecturers : 2
    Here I will need to store 5 students under each lecturers

    as for odd example,
    Num of students : 10
    Num of lecturers : 3

    Lecturer 1 will have 4 students
    Lecturer 2 will have 3 students
    Lecturer 3 will have 3 students

    how do i loop these 2 array of students and lecturers so I can insert the id of student inside each lecturer record? I am not sure how do I transfer this logic into codes..please.. i really need some idea,some example is very much appreciated
  • chathura86
    New Member
    • May 2007
    • 227

    #2
    try this one

    Code:
    <?php
    	$students = array('u1', 'u2', 'u3', 'u4', 'u5', 'u6', 'u7', 'u8', 'u9', 'u10');
    	$lecturers = array('l1', 'l2', 'l3');
    
    	$class = array();
    
    	$stdCount = count($students);
    	$lecCount = count($lecturers);
    
    	for($i=0; $i < $stdCount; $i++)
    	{
    		$class[$lecturers[$i%$lecCount]][] = $students[$i];
    	}
    
    	echo "<pre>";
    	var_dump($class);
    out put will be like this

    Code:
    array(3) {
      ["l1"]=>
      array(4) {
        [0]=>
        string(2) "u1"
        [1]=>
        string(2) "u4"
        [2]=>
        string(2) "u7"
        [3]=>
        string(3) "u10"
      }
      ["l2"]=>
      array(3) {
        [0]=>
        string(2) "u2"
        [1]=>
        string(2) "u5"
        [2]=>
        string(2) "u8"
      }
      ["l3"]=>
      array(3) {
        [0]=>
        string(2) "u3"
        [1]=>
        string(2) "u6"
        [2]=>
        string(2) "u9"
      }
    }

    Comment

    • nurul02
      New Member
      • Apr 2010
      • 7

      #3
      Originally posted by chathura86
      try this one

      Code:
      <?php
      	$students = array('u1', 'u2', 'u3', 'u4', 'u5', 'u6', 'u7', 'u8', 'u9', 'u10');
      	$lecturers = array('l1', 'l2', 'l3');
      
      	$class = array();
      
      	$stdCount = count($students);
      	$lecCount = count($lecturers);
      
      	for($i=0; $i < $stdCount; $i++)
      	{
      		$class[$lecturers[$i%$lecCount]][] = $students[$i];
      	}
      
      	echo "<pre>";
      	var_dump($class);
      out put will be like this

      Code:
      array(3) {
        ["l1"]=>
        array(4) {
          [0]=>
          string(2) "u1"
          [1]=>
          string(2) "u4"
          [2]=>
          string(2) "u7"
          [3]=>
          string(3) "u10"
        }
        ["l2"]=>
        array(3) {
          [0]=>
          string(2) "u2"
          [1]=>
          string(2) "u5"
          [2]=>
          string(2) "u8"
        }
        ["l3"]=>
        array(3) {
          [0]=>
          string(2) "u3"
          [1]=>
          string(2) "u6"
          [2]=>
          string(2) "u9"
        }
      }
      chatura,

      ive below codes to be modified so it can go along with ur logic..

      previously the original code is as :
      Code:
      $grader ='aa,bb,cc';
      $grader = explode(',', $grader);
      foreach($grader as $key => $grader)
      		 {
      		 	$subgradedetail["assgid"]=$_POST["aid"];
      		 	$subgradedetail["grader"]=$grader;
      			$assignment->setgraderdetail($subgradedetail);
      		 }
      
      function setgraderdetail($arr){
      //insert the details into database
      }
      and now i've modified it to:
      Code:
      $grader ='aa,bb,cc';
      $student='a,b,c,d,e';
      $stdCount = count($students);//will give 5
      $lecCount = count($graders);//will give 3
      $grader = explode(',', $grader);
      for ($i=0; $i<$stdCount ;$i++)
      {
      		 	$subgradedetail["assgid"]=$_POST["aid"];
      		 	$subgradedetail["grader"][$i%$lecCount]=$grader;
                              $subgradedetail["student"]=$student;
      			$assignment->setgraderdetail($subgradedetail);
      		 }
      * $subgradedetail["student"] refers to field student of table in the db

      but the new codes do no seem working..can u tell me which part i did it wrong?i appreciate ur initial idea btw..
      Last edited by Atli; May 2 '10, 12:39 PM. Reason: Added [code] tags.

      Comment

      • chathura86
        New Member
        • May 2007
        • 227

        #4
        Code:
        $stdCount = count($students);//will give 5
        $lecCount = count($graders);//will give 3
        $grader = explode(',', $grader);
        u are assigning the count before exploding the string

        explode both of them before getting the count

        Regards

        Comment

        • nurul02
          New Member
          • Apr 2010
          • 7

          #5
          Originally posted by chathura86
          Code:
          $stdCount = count($students);//will give 5
          $lecCount = count($graders);//will give 3
          $grader = explode(',', $grader);
          u are assigning the count before exploding the string

          explode both of them before getting the count

          Regards
          chat,
          thank u sooo much for ur great idea,now everything is working smoothly =)

          Comment

          Working...