0

i am trying to create a 3-level array then retrieve the 3th level array data but somehow i get this.

$project = array();
$project[] = "name";
$project[] = "id";
$project["id"] = "AXA";
$project["id"]["AXA"] = "a new project";
echo $project["id"]["AXA"];

The result i get is a which came from a new project

How do get the whole string?

Kermit
  • 33,827
  • 13
  • 85
  • 121
Genjo
  • 133
  • 1
  • 1
  • 8
  • `$project[] = "id"` actually means `$project[1] = "id"` so if your trying to set it like `$project['id'] = array()`, just use a multi-dimensional array format like `$project = array ('id'=>array('AXA'=>'a new project')); ` – zgr024 Mar 08 '13 at 03:20

2 Answers2

1

Your code should just use a multi-dimensional array as the assignment like the following

$project = array (
    'name',
    'id'=>array(
        'AXA'=>'a new project'
    ) 
);
zgr024
  • 1,175
  • 1
  • 12
  • 26
0

Here's a var_dump of your code:

array(3) {
  [0]=>
  string(4) "name"
  [1]=>
  string(2) "id"
  ["id"]=>
  string(3) "aXA"
}

You're not actually creating a new level. What you need to do is initialize the 2nd array:

$project = array();
$project[] = "name";
$project[] = "id";
$project["id"] = array(); //here
$project["id"]["AXA"] = "a new project";

Otherwise, it will write over the value AXA.

array(3) {
  [0]=>
  string(4) "name"
  [1]=>
  string(2) "id"
  ["id"]=>
  array(1) {
    ["AXA"]=>
    string(13) "a new project"
  }
}
Kermit
  • 33,827
  • 13
  • 85
  • 121