473,378 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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
Apr 30 '10 #1
4 1743
chathura86
227 100+
try this one

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     $students = array('u1', 'u2', 'u3', 'u4', 'u5', 'u6', 'u7', 'u8', 'u9', 'u10');
  3.     $lecturers = array('l1', 'l2', 'l3');
  4.  
  5.     $class = array();
  6.  
  7.     $stdCount = count($students);
  8.     $lecCount = count($lecturers);
  9.  
  10.     for($i=0; $i < $stdCount; $i++)
  11.     {
  12.         $class[$lecturers[$i%$lecCount]][] = $students[$i];
  13.     }
  14.  
  15.     echo "<pre>";
  16.     var_dump($class);
out put will be like this

Expand|Select|Wrap|Line Numbers
  1. array(3) {
  2.   ["l1"]=>
  3.   array(4) {
  4.     [0]=>
  5.     string(2) "u1"
  6.     [1]=>
  7.     string(2) "u4"
  8.     [2]=>
  9.     string(2) "u7"
  10.     [3]=>
  11.     string(3) "u10"
  12.   }
  13.   ["l2"]=>
  14.   array(3) {
  15.     [0]=>
  16.     string(2) "u2"
  17.     [1]=>
  18.     string(2) "u5"
  19.     [2]=>
  20.     string(2) "u8"
  21.   }
  22.   ["l3"]=>
  23.   array(3) {
  24.     [0]=>
  25.     string(2) "u3"
  26.     [1]=>
  27.     string(2) "u6"
  28.     [2]=>
  29.     string(2) "u9"
  30.   }
  31. }
  32.  
Apr 30 '10 #2
@chathura86
chatura,

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

previously the original code is as :
Expand|Select|Wrap|Line Numbers
  1. $grader ='aa,bb,cc';
  2. $grader = explode(',', $grader);
  3. foreach($grader as $key => $grader)
  4.          {
  5.              $subgradedetail["assgid"]=$_POST["aid"];
  6.              $subgradedetail["grader"]=$grader;
  7.             $assignment->setgraderdetail($subgradedetail);
  8.          }
  9.  
  10. function setgraderdetail($arr){
  11. //insert the details into database
  12. }
and now i've modified it to:
Expand|Select|Wrap|Line Numbers
  1. $grader ='aa,bb,cc';
  2. $student='a,b,c,d,e';
  3. $stdCount = count($students);//will give 5
  4. $lecCount = count($graders);//will give 3
  5. $grader = explode(',', $grader);
  6. for ($i=0; $i<$stdCount ;$i++)
  7. {
  8.              $subgradedetail["assgid"]=$_POST["aid"];
  9.              $subgradedetail["grader"][$i%$lecCount]=$grader;
  10.                         $subgradedetail["student"]=$student;
  11.             $assignment->setgraderdetail($subgradedetail);
  12.          }
  13.  
* $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..
May 2 '10 #3
chathura86
227 100+
Expand|Select|Wrap|Line Numbers
  1. $stdCount = count($students);//will give 5
  2. $lecCount = count($graders);//will give 3
  3. $grader = explode(',', $grader);
u are assigning the count before exploding the string

explode both of them before getting the count

Regards
May 2 '10 #4
@chathura86
chat,
thank u sooo much for ur great idea,now everything is working smoothly =)
May 3 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Javi | last post by:
I have some doubts about what is the best method to distribute classes in .cpp and .h files: - Should I use a file per class? or should I group similar classes in one file? - Is it good to put a...
4
by: Ed Landau | last post by:
Hi: I've just developped a really simple MS Access App. The VBA runs within the database. It uses some DLLs which I register with a script. It's on MS Access2003 (Office11). I've got 2003Pro...
8
by: Larry__Weiss | last post by:
What kind of licensing is needed to be able to use the Microsoft Access product and distribute the applications? In other words, what version of Microsoft Access does a developer need to buy in...
4
by: Darius | last post by:
How to distribute a C program into many C files ? this is the first question a C programmer asks after learning C basics, Questions like --what is a header file --where should i put global...
1
by: Yogi_Bear_79 | last post by:
I recently some embedded GZIP files into my project. I added the sharpziplib.dllas a referance. But when I distribute the .exe it looks for the .dll. Did I do something wrong, or miss step? I...
10
by: Doug Crabtree | last post by:
I am kinda new to the whole distribution process. I several programs that I am writing that use common .NET DLLs. I want each program to use the same DLLs in a "COMMON" folder. How do I get...
1
by: akbar | last post by:
Hi, I am creating not-so-important opensource application written in python for Linux. I have two files python source in src directory, named blabla1.py and blabla2.py. There are byte compiled...
6
by: Salman | last post by:
I would like to know how I can distribute the application that I create with Visual C++ express edition. I checked the menu options to find a deploy option similar to the one found on the Visual...
0
by: hooked888 | last post by:
Hi, I have given a task but i have no idea on it.. it sound simple but in store procedure i can't have a clue at all. Table Water NameOfWater VARCHAR, WaterAmount INT Table Cup Id int,
11
by: jackbenimble999 | last post by:
Hello! I'd like to distribute an application developed in MS Access 2003. I've looked at the MS web site and it says although the ability to distribute is included with MS Office Retail, you...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.