douyao6842
2013-05-24 19:05
浏览 95
已采纳

PHP中的多维数组 - 添加额外的维度?

$counter = 0;
foreach($dates as $dbRow) :
$datesExp[] = [$counter => array($dbRow->e_id => new DateTime($dbRow->l_date_expect))];
++$counter;
endforeach;

Using the above code, I'm trying to achieve is an array in format of:

0 => array(
    PK => DATE)
1 => array(
    PK2 => DATE2)
....etc

But what i'm getting with a var_dump is:

array (size=3)
    0 => 
    array (size=1)
      0 => 
        array (size=1)
          2 => 
            object(DateTime)[7]

Where is this: array (size=1) 0=> intermediate dimension coming from?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doucong4535 2013-05-24 19:10
    已采纳

    From here: $datesExp[] = [$counter => array($dbRow->e_id => new DateTime($dbRow->l_date_expect))];

    $datesExp[] = = add the following as an entry in this array

    [...]; = everything in here is an array

    array($dbRow->e_id => new DateTime($dbRow->l_date_expect)) = the innermost array

    should be

    $datesExp[$counter] = array($dbRow->e_id => new DateTime($dbRow->l_date_expect));

    打赏 评论
  • dongpa5277 2013-05-24 19:13

    try this ,no need of seperate counter,no need of outside braces [..]

    $datesExp[] = array($dbRow->e_id => new DateTime($dbRow->l_date_expect));
    
    打赏 评论

相关推荐 更多相似问题