将静态多维数组转换为动态数组

时间:2012-12-13 12:22:05

标签: php

我有这个静态数组:

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

我想变成一个动态数组(具有相同的元素)。

由于某种原因,以下代码不好:

  $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");

为什么不好? 我该如何解决?

2 个答案:

答案 0 :(得分:1)

你可以这样做:

$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");

通常$array = array('key' => 'value');$array['key'] = 'value';相同。

另请查看documentation

答案 1 :(得分:1)

应该是:

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