The example shows how to establish a dictionary of (row, column):value pairs to mimic a two dimensional array. This can be easily expanded to more dimensions. Python3 has dictionary comprehension, making the process of creating a dictionary of this type easier.

Member Avatar
Member Avatar
+1 forum 9

At times, I have need to push all submitted form array values into $_SESSION. To do this, I've written a small function: function addPostToSession() { foreach ($_POST as $key=>$value ){ $_SESSION[$key]=stripslashes(strval($value)); } } This works well, as long as $_POST is one dimensional, but in a form with checkboxes, $_POST may contain arrays as array elements. Example: POST: Array ( [othertype] => Server [issues] => Array ( [0] => crashes [1] => freezes ) ) When I use my addPostToSession() function, $_SESSION['issues'] is given the string 'Array' (checked with var_dump). Example: SESSION: Array ( [othertype] => Server [issues] => Array …

Member Avatar
Member Avatar
+0 forum 4

Hi all. Have been racking my brains about how to go about creating a multidimensional array dynamically from an array of keys. Something like... $keys = array('key1', 'key2', 'key3'); $value = 'some value'; // $md = ...???!!!... I need the $md multidimensional array to have the keys thus... $md['key1']['key2']['key3'] = $value; Obviously that could be hardcoded quite easily, but my problem it that the $keys array is variable - it can hold different values and the number of items can vary. So how to build an md array? I'm looking for a generic routine. I've come close a few times, …

Member Avatar
Member Avatar
+0 forum 9

So I'm basically trying to recreate the simulation of two dice being rolled, and I need to count the frequency in a two dimensional rectangular array, then display the array. I'm so lost I wrote some code but it didn't work, can anyone help. This is the assignment I'm not looking to cheat because I've done some work, I'm just stuck. reate a C# application to simulate the rolling of two dice. Use a Random object to generate a random integer in the range 1 to 6 for each die. There are 36 possible combinations (6 possible numbers for die …

Member Avatar
Member Avatar
+0 forum 8

So im making an RPG. For collision detection, I thought I would do it like this: 1. Read text file containing collision data. 2. Iterate through the list using nested for loops. 3. When a value of 1 occurs (denoting an inpassable tile) add a rectangle to list 'badTiles', where the x and y pos of the rectangle is the x and y value from the nested for loop times the tile width and height. 4. Now that all bad tiles are stored in a list of rectangles, we can check for collision. 5. In player class, perform a foreach …

Member Avatar
Member Avatar
+0 forum 9

So, Im making a game in c# using Xna. I have a function that will read a text file and display a map on screen, using numbers in the file as texture references. The file is a integer multidimensional array. I have another file which is used to set collision bounds, called collisionMap.txt. My thinking was this: 1. Read collision map file 2. If any value within the array equaly '1', then that tile will be inpassable. 3. Get x and y positions of that wile within the array. 4. These inpassable tiles will be added to another array containing …

Member Avatar
Member Avatar
+0 forum 2

Please consider the following class : <?php class p0110dbinsertbuilder { public $table=""; private $fields ; public function add($Field,$Value){ $this->fields = array($Field,$Value); } public function insertstring(){ $count = count($this->fields,0); $c = "INSERT INTO " . $this->table . "("; for ($i=0;$i<$count;$i++){ $c .= $this->fields[$i][0]; if ($i <> $count-1){ $c .= ","; } } $c .= " ) VALUES ( "; for ($i=0;$i<$count;$i++){ $c .= $this->fields[$i][1]; if ($i <> $count-1){ $c .= ","; } } $c .= ")"; return $c; } } ?> and calling it as follows: $o = new p0110dbinsertbuilder(); $o->table = "suppliers"; $o->add("supplier_name",'"SuppplierName"'); echo $o->insertstring(); I have 2 questions : …

Member Avatar
Member Avatar
+0 forum 1

hi i am really new to this and can't see why i can't get my code to work, it is probily really silly but help would be appreciated. i am trying to get the user to enter the number of competators of a 4 lap race so that it can determine the lap size then read in the lap times of each competator. # include <stdio.h> # include <malloc.h> /* define the structure , what you want the user to input*/ struct laptimes { float lap1; float lap2; float lap3; float lap4; }; int main (){ int comp ; int …

Member Avatar
Member Avatar
+0 forum 21

//Give me an example for Serialising Multidimensional array in c sharp. //While searching in net i found this code snippet [XmlIgnore] public char[,] Data; [XmlElement("Data")] [EditorBrowsable(EditorBrowsableState.Never)] public char[][] XmlData { get { /* copy contents of Data into a jagged array */ } set { /* copy contents of jagged array into Data */ } } //Please explain this in detail.

Member Avatar
Member Avatar
+0 forum 1

Hi! I am learning C, and am at very introductory level, using this book called C Programming: A Modern Approach. I have just studied about arrays, and while tackling the programming exercises,i got stuck in Que 9 in Chapter 8. This is what the question says [INDENT]Write a program that generates a "random walk" across a 10 x 10 array. The array will contain characters (all '.' initially).The program must "walk" from element to element, always going up,down,left, or right by one element. The elements visited will be labelled with the letters A through Z, in the order visited. Here …

Member Avatar
Member Avatar
+0 forum 13

>Hi guys,..i've two arrray data1 and data2 then i do intersect to get same value between data1 and data2, and >use count to get total same value,..how to ARSORT count result from intersect array multidimensional? <?php $data1 = array( array( '7' => 'chelsea everton', '8' => 'everton villa', '9' => 'villa liverpool', '10' => 'liverpool manutd' ), array( '23' => 'milan inter ', '24' => 'inter juventus', '25' => 'juventus napoli', ), array( '53' => 'chelsea everton', '54' => 'barcelona altetico', '55' => 'altetico getafe', '56' => 'everton villa', ) ); $data2 = array( array( '3' => 'milan inter', '4' …

Member Avatar
Member Avatar
+0 forum 2

>hi guys, how to get first and last (key and value) array multidimesional below? $data1 = array( array( '0'=>array( '7'=>'chelsea', '8'=>'everton', '9'=>'villa', '10'=>'liverpool' ), array( '23'=>'milan', '24'=>'inter', '25'=>'juventus', ), ), '1'=>array( array( '53'=>'madrid', '54'=>'barcelona', '55'=>'altetico', '56'=>'everton', ), array( '243'=>'milan', '244'=>'inter', '245'=>'juventus', ), ), '2'=>array( array( '523'=>'madrid', '524'=>'barcelona', '525'=>'altetico', '526'=>'everton', ), array( '223'=>'milan', '224'=>'inter', '225'=>'juventus', ), ) ); i want yield like this: Array ( [0] => Array ( [0] => Array ( [7] => chelsea [10] => liverpool ) [1] => Array ( [23] => milan [25] => juventus ) ) [1] => Array ( [0] => Array ( [53] …

Member Avatar
Member Avatar
+0 forum 2

I have the following code in my **PHP** program: $query = "SELECT * FROM board_members"; $result = mysql_query($query) or die(mysql_error()); $data = array(); // create a variable to hold the information while (($row = mysql_fetch_array($result, MYSQL_ASSOC)) !== false){ $data[] = $row; // add the row in to the results (data) array } print_r($data); // print result This give me the following corrrect data for the table: Array ( [0] => Array ( [FirstName] => Alona [LastName] => Burnett [Amount] => 1000.00 ) [1] => Array ( [FirstName] => Anna Marie [LastName] => Hartman [Amount] => 1200.00 ) [2] => Array …

Member Avatar
Member Avatar
+0 forum 2

I have an array of file names which gets split into 3's using array_chunk, then using array_push "X" is added to each array chunk. I now want to to reassemble the array chunks into one "2D" array. I have been playing around with array_merge, but I can only get the last array chunk to merge over and over. So... I have an array ($filesOUT) that looks like this: [CODE] Array ( [COLOR="red"][0] => DSCF2361A1.jpg [1] => DSCF2373.JPG [2] => DSCF2383.JPG [3] => X [/COLOR] ) Array ( [COLOR="red"][3] => DSCF2347.JPG [4] => DSCF2335.JPG [5] => DSCF2366.JPG [6] => X [/COLOR] …

Member Avatar
Member Avatar
+0 forum 8

I am working on a project that requires a CSV file to be read into a multidimensional array. I loop through the array, and set multidimensionalarray[x][n] to the corresponding data in the CSV file, but I keep getting IndexError: list index out of range. I can't figure out why, and I've tried a number of different ways to fix it. Naturally, I took to the internet and found several forum posts that were almost, but not quite applicable to this. The code is as follows: [CODE]def load(): x = 0 if not os.access('Database.csv', os.F_OK): commands.getoutput('touch Database.csv') file = csv.reader(open('Database.csv', 'rb')) …

Member Avatar
Member Avatar
+0 forum 2

I have this example php string: [CODE]$string = "@[item_1][door] @[mozart][grass] = yes @[mozart][green] = no @[mozart][human] @[blue][movie]=yes @[item_1][beat] = yes @[item_1][music] = no ";[/CODE] now $string idented just to easy view: [CODE]@[item_1][door] @[mozart][grass] = yes @[mozart][green] = no @[mozart][human] @[blue][movie]=yes @[item_1][beat] = yes @[item_1][music] = no[/CODE] I want to know how can i get this string ( or other string following this style ) and transform in an array that looks like: [CODE]Array ( [item_1] => Array ( [door] => Array ( [mozart] => Array ( [grass] => yes [green] => no [human] => Array ( [blue] => Array ( …

Member Avatar
Member Avatar
+0 forum 5

Hi i need a little help. i want to read a multi array from a file but code blocks said a lots of error and i dont know whats the problem. can u help me? [ICODE]#include <iostream> #include <cstdlib> #include <math.h> #include <fstream> #include <sstream> using namespace std; int maxn=100; int maxm=100; void beolv(string filename, int &n, int &m, int x[maxn][maxm]) { //fájl megnyitása ifstream fin(filename.c_str(), ios::in); fin >> n; fin >> m; for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { fin >> x[i][j]; } } fin.close(); } int main() { int n; int m; int …

Member Avatar
Member Avatar
+0 forum 2

Dear All, I have created a multi dimensional array and stored elements in that through while loop like $human = array(); and at every iteration the while loop will add elements like $human["IPI"]["Description"][] = $row; $human["IPI"]["link"][]= $link; but when i wanted to print this array contents in a table format i wrote it like this inside the table environment: [CODE] for ($i=0;$i<=$j;$i++) { echo '<tr>'; echo "<td>Chromosome</td><td>$human[IPI][Description][$i] ($human[IPI][link][$i])</td><td>$mouse[Chromosome]</td>"; echo '</tr>'; }[/CODE] but it displays like Array[Description][0] (Array[link][0]) in the output table. I would be thankful if anyone could help me with this issue. Thanks in advance. cheers, Aravind

Member Avatar
Member Avatar
+0 forum 6

Hello people :) i'm trying to insert query result into 2 dimensional array i have tried the following way.. [CODE] $query=mysql_query("..."); while($row=mysql_fetch_array($query)){ for($i=0;$i<=$index;$i++){ if(isset($myarray[$i]['array1'])|| isset($myarray[$i]['array2'])){ array_unshift($myarray[$i]['array1'],$row['study_period']); array_unshift($myarray[$i]['array2'],$row['test']); }//endif }//endfor }//endwhile [/CODE] But, it failed i also tried.. [CODE] $query=mysql_query("..."); while($row=mysql_fetch_array($query)){ $myarray=array( array("array1"=>$row['study_period'],"array2"=>$row['test']) ); }//endwhile [/CODE] it failed too since it only take the first index of array(myarray[0][]). I think i'll use the second but i need the array to increment its index as in the first. i wonder how to do this with the second way. I hope i made my question clear :D Thank you :)

Member Avatar
Member Avatar
+0 forum 6

Hello all! :) Do you have any link on some complete reference on multidimensional array in php?? Please tell me.. thanks a lot.

Member Avatar
Member Avatar
+0 forum 5

Hi, I'm new to this forum and C#. I try to make an array that each is the sum of the value of each row in sims array. It gives me nothing but zero. Please help. TIA. [CODE]public double[] ave_S(int M, int N, double S, double sig, double dt, double[,] R, double r) { double[,] sims = GetSim(M, N, S, sig, dt, R, r); double[] temp = new double[M]; for (int i = 0; i < M; i++) { double temp_val = 0; for (int j = 0; j < N; j++) { temp_val += sims[i, j]; } temp[i] = …

Member Avatar
Member Avatar
+0 forum 3

Hi all I was wondering how to pass a multidimensional array to a function: Here is my code: [CODE] #include <cstdlib> #include <iostream> using namespace std; void PrintArray (int* array, int n); int main(){ int SIZE; cout << "Enter how many records: "; cin >> SIZE; int array[SIZE][2]; for (int i =0; i < SIZE; i++){ cout << "Enter an Integer: "; cin >> array[i][0]; cout << "Enter a user ID: "; cin >> array[i][1]; } cout << endl << "The list you put in is: " << endl; PrintArray(array, SIZE); system("PAUSE"); } /* This function prints an Array Arguments: …

Member Avatar
Member Avatar
+0 forum 9

Hello DaniWeb! I'm trying to return an array of strings from a function but I'm not sure how to do so. I've done the prerequisite Googling and have found people on this discussion board recommending to return char** which, it is said, can then be converted back into 2d array notation. It is this conversion to and from char** which has confused me and I have not been successful in clarifying this for myself, so my question is this: How may I represent a 2D array in a fashion that can be returned as char**, and how would I re-convert …

Member Avatar
Member Avatar
+0 forum 3

Hello I want to pass a bidimensional vector to the constructor of all instances of [ICODE]MyClass[/ICODE]. All instances must use and share and modify the same vector. And I want to keep a reference to that [ICODE]main_vector[/ICODE] to use it later in the program I am passing it by reference to MyClass. The problem is that when I assign it to [ICODE]ref_to_main_vector[/ICODE] to use it later, I'm not saving the reference but creating a duplicate of it, so each instances has its own copy. How can I make them all share the same variable? I'm not interested in passing it …

Member Avatar
Member Avatar
+0 forum 7

I was trying to code the assembly line sheduling problem using the dynamic programming approach. I have coded the program but i am facing problem with the 2-dimensional arrays I am using. For testing purpose, I wanted to initialize the array, but I was not able to get it. Moreover, as most of the arrays I am using are dynamically allocated, They were not getting passed to functions. Though I have made the program working with some tricks but I would really appreciate if someone helps me improve it. [CODE]#include<iostream> using namespace std; //Had to make much of the variables …

Member Avatar
Member Avatar
+0 forum 3

Hey y'all, I have been working on a tensor library (Nth-order multi-dimensional arrays) and I've been having trouble coming up with a good scheme for a multi-dimensional iterator template. Basically, I have a class template "tensor_container" which stores all the values of the N-order array (as a std::vector of one dimension) and the size of each dimension. The values themselves are thus serialized as a 1D vector with the lowest level being continuous (e.g. a second order tensor would correspond to a column-major matrix). Now, I pretty much managed to get the indexing to work just fine with a few …

Member Avatar
Member Avatar
+0 forum 3

I am trying to define a 2D array of 1000 by 1000 ints and then test which transversal method is faster (row major or column major). I have this written [CODE] #include <stdio.h> #include <time.h> int main() { clock_t start, end; double elapsed; start=clock(); int array[1000][1000]; int row, col; for(row=0; row<1000; row++) for(col=0; col<1000; col++) array[row][col]= row*col; end=clock(); elapsed= (end-start)/(double)CLOCKS_PER_SEC; printf("Seconds elapsed = %f\n", elapsed); printf("%d\n", array); return 0; [/CODE] The program compiles, but causes an exception while running. I've tried with smaller int sizes (10, 100, etc) but they all yield times of 0.000000 seconds, like the array is …

Member Avatar
Member Avatar
+0 forum 5

I cant figure out what is wrong with this code These are the instructions [ATTACH]17304[/ATTACH] [ATTACH]17305[/ATTACH] [ATTACH]17306[/ATTACH] and this is my code [CODE]#include<iostream> #include<fstream> #include<iomanip> using namespace std; void Get_Info(string[],int[][3]); void Get_Average(int[][3]); void Prt_GradeBook(string[],int[][3],int[]); int MakeUps(int[][3]); int Grades[5][3],average[5]; string Students[5]; int main() { Get_Info(Students,Grades); Get_Average(Grades); Prt_GradeBook(Students,Grades,average); cout<<"There are"<<MakeUps(Grades)<<"Makeup(s) to arrange. "<<endl; system("pause"); return 0; } void Get_Info(string[5],int[5][3]) { int i, j; ifstream infile; infile.open("Cosc 117 Test Results.txt"); while(infile) { infile>>Students[i]; } for(i=0;i<5;i++) { for(j=0;j<3;j++) { infile>>Grades[i][j]; } } infile.close(); } void Get_Average(int[5][3]) { int i,j,sum; for(i=0;i<5;i++) { sum=0; for(j=0;j<3;j++) { sum+=Grades[i][j]; } average[i]=sum/3; } } void Prt_GradeBook(string[5],int[5][3],int[5]) { int i, …

Member Avatar
Member Avatar
+0 forum 9

Hello, In OOP PHP, i got 2 classes: Database and News. In News class i call DB function and get result (multidimensional 2D array). Now i have to display that array in index.php. I call News class (lets say get_news() function) from index.php but cant retrieve data and always get message: "Cannot use object of type News as array in ..." Here is some of my code: database.php [CODE]class DB{ function news($user_id){ $q="select all from news where author id='$id'"; $result=$db->query($q); for($i=0; $i<$r->num_rows; $i++){ $arr[]=$result->fetch_array; } return $arr; //works fine! }}[/CODE] news.php [CODE]class News{ function get_news($user_id){ global $db; $news=$db->news($user_id); print_r($news); // …

Member Avatar
Member Avatar
+0 forum 4

I have written a program that will generate a random maze and solve it. The program uses a multidimensional array of a class that holds bool values for each wall, whether or not to show an * for the path taken, and if it is a dead end. here is the class [code=c]#include <cstdlib> using namespace std; class MazeCell { public: // ============================================================================================ // Routine: Class Constructor // Purpose: Allows setting the default values of the class when instantiated by the host. // ============================================================================================ MazeCell() { wallNORTH = false; wallEAST = false; wallSOUTH = false; wallWEST = false; path = …

Member Avatar
Member Avatar
+0 forum 1

The End.