473,508 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create Multidimensional Arrays?

26 New Member
hi

i got all data in single array. How i can store the data in multidimensional array?

below my code:
Expand|Select|Wrap|Line Numbers
  1.     require_once('../../includes/common.php');
  2.  
  3.         $m = array();
  4.     $result = mysql_query("SELECT * from sgc where year = '2008'") or die(mysql_error());
  5.  
  6.     while($row = mysql_fetch_array($result))
  7.         {
  8.          // echo $row['nric']."<br>";
  9.           $res =mysql_query("select COURSE_TITLE_LONG, CRSE_GRADE_OFF, UNT_EARNED, nric from ite where nric = '".$row['nric']."' AND STRM = '2008'")or die(mysql_error()); 
  10.           $norows=mysql_num_rows($res);
  11.           echo $norows."<br>";
  12.  
  13.           while($rows = mysql_fetch_array($res))
  14.           {
  15.               for($i=0;$i<$norows;$i++){
  16.               $data = array("course" => "'".$rows['COURSE_TITLE_LONG']."'" , "Grade" => "'".$rows['CRSE_GRADE_OFF']. "'" , "Unit" => "'".$rows['UNT_EARNED']."'");
  17.               }
  18.               $ser = serialize($data);
  19.               print $ser;
  20.               echo "<br>";
  21.           }
  22.         }
  23.  
  24.  
i got this output, kindly help to make multidimensional array
Expand|Select|Wrap|Line Numbers
  1. a:3:{s:6:"course";s:24:"'Personal Effectiveness'";s:5:"Grade";s:3:"'S'";s:4:"Unit";s:3:"'3'";}
  2. a:3:{s:6:"course";s:16:"'Basic Numeracy'";s:5:"Grade";s:3:"'U'";s:4:"Unit";s:3:"'2'";}
  3. a:3:{s:6:"course";s:23:"'Piping & Valve System'";s:5:"Grade";s:3:"'D'";s:4:"Unit";s:3:"'6'";}
  4. a:3:{s:6:"course";s:24:"'Mechanical Fabrication'";s:5:"Grade";s:3:"'F'";s:4:"Unit";s:3:"'6'";}
  5.  
Mar 11 '11 #1
2 2355
dgreenhouse
250 Recognized Expert Contributor
You normally serialize the data to insert into a database but you can use the serialized data to transmit over a network.

What do want the multidimensional array structure to look like?

PHP's multidimensional arrays are easy...

$array[0][0]
...
$array[10][10]

$array[0]['name']
$array[0]['address']
$array[0]['phone']
...
$array[10]['name']
$array[10]['address']
$array[10]['phone']

Expand|Select|Wrap|Line Numbers
  1. $ser_index = 0;
  2.  
  3. while($row = mysql_fetch_array($result))
  4.         {
  5.          // echo $row['nric']."<br>";
  6.           $res =mysql_query("select COURSE_TITLE_LONG, CRSE_GRADE_OFF, UNT_EARNED, nric from ite where nric = '".$row['nric']."' AND STRM = '2008'")or die(mysql_error()); 
  7.           $norows=mysql_num_rows($res);
  8.           echo $norows."<br>";
  9.  
  10.           while($rows = mysql_fetch_array($res))
  11.           {
  12.               for($i=0;$i<$norows;$i++){
  13.               $data = array("course" => "'".$rows['COURSE_TITLE_LONG']."'" , "Grade" => "'".$rows['CRSE_GRADE_OFF']. "'" , "Unit" => "'".$rows['UNT_EARNED']."'");
  14.               }
  15.               $ser[$ser_index++] = serialize($data);
  16.           }
  17.         }
  18. print_r($ser);
  19.  
Mar 11 '11 #2
apssiva
26 New Member
Hi,

I retrieve data in the DB. I create a array and make it serialize. Below code was retrieve data each line in one array. but i want all lines in one array or array of array.

for ex:
array(array(course,grade,unit),array(course,grade, unit),.......)

if get in array use count()-> retrieve the data easy.

Expand|Select|Wrap|Line Numbers
  1. while($rows = mysql_fetch_array($res))
  2.           {
  3.               for($i=0;$i<$norows;$i++){
  4.               $data = array("course" => "'".$rows['COURSE_TITLE_LONG']."'" , "Grade" => "'".$rows['CRSE_GRADE_OFF']. "'" , "Unit" => "'".$rows['UNT_EARNED']."'");
  5.               }
  6.               $ser = serialize($data);
  7.               print $ser;
  8.               echo "<br>";
  9.           }
  10.  
If i store the data line by line array(course,grade,unit) in the database, can't retrieve all the data at a time. kindly help.... i am trying to solve this problem more than three days..... still can't please help... (because i am new for php)

retrieve code
Expand|Select|Wrap|Line Numbers
  1.     require_once('../../includes/common.php');
  2.     $m = array();
  3.  
  4.     $r = '';
  5.         $result = mysql_query("SELECT meta_ite from sgc where nric = 'S9405127D'") or die(mysql_error());
  6.         $count = mysql_numrows($result);
  7.  
  8.     while($row = mysql_fetch_array($result))
  9.        {
  10.           $data = $row['meta_ite'];
  11.                   print_r (count($data));
  12.                    $val = unserialize($data);
  13.                  foreach($val as $key => $va){
  14.                  print $va;
  15.                  }
  16.  
  17.       }
  18.  
  19.  
output:
Expand|Select|Wrap|Line Numbers
  1. English,A,3
  2.  
Mar 11 '11 #3

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

Similar topics

6
15559
by: ubccis | last post by:
Hi. Does for each for on multidimensional arrays? For example, if one element of $in is $course_list and I want to echo all everything in course_list
2
1137
by: Terry | last post by:
Hi, can someone plz tell me how multidimensional arrays (like a 2-D array) are stored in memory? Are they like single dimensional arrays? Stored sequentially in one "row", so to say? Thanks ...
9
6653
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
5
545
by: whisper | last post by:
Hi: there is an issue that confuses me and the FAQ did not clarify it for me. (Sorry I am just learning!) Let say I define a multidimensional array in my main routine as follows: int...
3
1859
by: matthewburton | last post by:
Hi everyone, I'm trying to write a program that will search a body of text and replace certain words (say, A, B, and C) with different words that I think are more appropriate (A1, B1 and C1). I...
21
4152
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
9
2117
by: barmy_mad | last post by:
Hi I new to C# and trying to store/extract a collection of multidimensional arrays. For example; string a_DATA = new string object o_CON = new object for(int i=0;i<10;i++){ for(int...
5
1968
by: asdf | last post by:
I was told not to use the low-level language such as arrays which inherited from C, I want to know what can I use to substitute the C-style multidimensional arrays? Is there multidimensional vector?
2
4470
by: oopsatwork | last post by:
Ok...so, I have been outside of the C world for a _very_ long time...but not so long as to remember how to do multidimensional arrays. So, let me state that I know how to malloc pointers to...
12
5619
by: filippo nanni | last post by:
Hello everybody, my question is this: I have two multidimensional arrays and I have to create a third one (for later use) from comparing these two. Here is my example code: //BEGIN CODE var...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.