devmonster
devmonster

Reputation: 1739

turning static multidimensional array into a dynamic one

I have this static array:

$elems = array(
  date($format, strtotime("12-12-12"))
     => array(
            "Title" => "title1",
            "Color" => "color1"),
  date($format, strtotime("12-12-11"))
     => array(
            "Title" => "title2",
            "Color" => "color2"),
);

which I want to turn into a dynamic array (with the same elements) .

for some reason the following code isn't good:

  $elems = array();
  $elems[] = date($format, strtotime("12-12-12"))
     => array(
            "Title" => "title1",
            "Color" => "color1");
  $elems[] = date($format, strtotime("12-12-11"))
     => array(
            "Title" => "title2",
            "Color" => "color2");

why isn't it good? and how should I fix it?

Upvotes: 1

Views: 113

Answers (2)

Leri
Leri

Reputation: 12535

You can do:

$elems = array();
$elems[date($format, strtotime("12-12-12"))] = array(
        "Title" => "title1",
        "Color" => "color1");
$elems[date($format, strtotime("12-12-11"))] = array(
        "Title" => "title2",
        "Color" => "color2");

And in general $array = array('key' => 'value'); is the same as $array['key'] = 'value';.

Also take a look at documentation.

Upvotes: 1

xdazz
xdazz

Reputation: 160863

It should be:

$elems = array();
$elems[date($format, strtotime("12-12-12"))] =  array(
    "Title" => "title1",
    "Color" => "color1"
);

Upvotes: 1

Related Questions