Jump to content

[PHP5] Declaring 2D arrays


Drezard

Recommended Posts

You do not have to declare arrays in PHP... but assigning them, there are several ways

 

$arr[] = 'test1';

$arr[] = 'test2';

 

will populate $arr as such:

 

$arr[0] contains 'test1';

$arr[1] contains 'test2';

 

You can also use the array() function

 

$arr = array('test1', 'test2');

 

will populate $arr as such:

 

$arr[0] contains 'test1';

$arr[1] contains 'test2';

 

Alternately, you can define them with user keys with the array function

 

$arr = array('keyA' => 'test1', 'keyB' => 'test2');

 

will populate $arr as such:

 

$arr['keyA'] contains 'test1';

$arr['keyB'] contains 'test2';

 

You can also define them directly, as so

 

$arr['keyA'] = 'test1';

$arr[1] = 'test2';

 

To iterate through an array, the easiest way is to use the foreach() function, as described here

http://php.net/foreach

Link to comment
Share on other sites

He's asking for 2 dimensional (2D) arrays.

 

Basically, you don't instantiate them :P

 

All kidding aside, my preferred method is the following:

 

<?php

$d2 = array("dimension2" => "this is dimension2");
$d1 = array("dimension1" => $d1);

?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.