Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I declare a two dimensional array?

Tags:

arrays

php

What's the easiest way to create a 2d array. I was hoping to be able to do something similar to this:

declare int d[0..m, 0..n] 
like image 508
Mask Avatar asked Nov 28 '09 00:11

Mask


People also ask

How are 2 dimensional arrays declared and initialized?

In 2-D array we can declare an array as : Declaration:- Syntax. : data_type array_name[row_size][column_size]; Ex:- int arr[3][3]; where first index value shows the number of the rows and second index value shows the number of the columns in the array.

How do you declare a two-dimensional array in C++?

To declare a 2D array, use the following syntax: type array-Name [ x ][ y ]; The type must be a valid C++ data type. See a 2D array as a table, where x denotes the number of rows while y denotes the number of columns.


2 Answers

You can also create an associative array, or a "hash-table" like array, by specifying the index of the array.

$array = array(     0 => array(         'name' => 'John Doe',         'email' => '[email protected]'     ),     1 => array(         'name' => 'Jane Doe',         'email' => '[email protected]'     ), ); 

Which is equivalent to

$array = array();  $array[0] = array(); $array[0]['name'] = 'John Doe'; $array[0]['email'] = '[email protected]';  $array[1] = array(); $array[1]['name'] = 'Jane Doe'; $array[1]['email'] = '[email protected]'; 
like image 88
Atli Avatar answered Sep 26 '22 02:09

Atli


The following are equivalent and result in a two dimensional array:

$array = array(     array(0, 1, 2),     array(3, 4, 5), ); 

or

$array = array();  $array[] = array(0, 1, 2); $array[] = array(3, 4, 5); 
like image 41
David Snabel-Caunt Avatar answered Sep 27 '22 02:09

David Snabel-Caunt