array_push创建新数组而不是添加

时间:2013-01-22 12:38:44

标签: php arrays array-push

我想根据if语句将key =>值对添加到现有数组中。 但它不断添加key =>值对作为新索引。

这是我的代码:

foreach ($messageRepo as $oneMessage) {
        $calculatedTime = $oneMessage->getTimeToLive();
        $creationTime = $oneMessage->getCrdate();

        $calculatedTimes = $this->androidService->renderFormat($calculatedTime);
        $expiringDate = $creationTime + $calculatedTime;

        $ausgabe[] = array(
            'Time' => key($calculatedTimes),
            'Format' => current($calculatedTimes),
            'Title' => $oneMessage->getMessageTitle(),
            'Text' => $oneMessage->getMessageText(),
            );

        if (time() > $expiringDate) {
           array_push($ausgabe[]['Expired'], $expiringDate);

            } else {
            array_push($ausgabe[]['Expiring'], $expiringDate);
            }   
    }

转储说:

    array(60 items)
0 => array(4 items)
  Time => 0 (integer)
  Format => 'Stunden' (7 chars)
  Title => '3 wochen total neu' (18 chars)
  Text => 'dfdsfsdf fgdsfgdsgf' (19 chars)
1 => array(1 item)
  Expired => NULL

但我希望Expired => NULL作为原始索引中的字段而不是新索引。

3 个答案:

答案 0 :(得分:3)

在这种情况下,您不应该使用array_push。改为使用简单的赋值。由于您不知道要添加的元素的索引,因此可以创建新数组,设置其所有值,然后将其添加到整个数组中。像这样:

foreach ($messageRepo as $oneMessage) {
        $calculatedTime = $oneMessage->getTimeToLive();
        $creationTime = $oneMessage->getCrdate();

        $calculatedTimes = $this->androidService->renderFormat($calculatedTime);
        $expiringDate = $creationTime + $calculatedTime;

        $newval = array(
            'Time' => key($calculatedTimes),
            'Format' => current($calculatedTimes),
            'Title' => $oneMessage->getMessageTitle(),
            'Text' => $oneMessage->getMessageText(),
            );

        if (time() > $expiringDate) {
           $newval['Expired'] = $expiringDate;
        } else {
           $newval['Expiring'] = $expiringDate;
        }   

        $ausgabe[] = $newval;
}

答案 1 :(得分:1)

您正在使用array_push 和<{em>一起使用<{1}}表示法,它会执行相同的操作。最终结果是创建一个新元素,然后将 作为一个数组处理并将另一个新元素放入其中。

您永远不需要直接使用$array[]。你应该使用这样的东西:

array_push

将半成品新数组插入// This is what the new array inside $ausgabe will look like $newItem = array( 'Time' => key($calculatedTimes), 'Format' => current($calculatedTimes), 'Title' => $oneMessage->getMessageTitle(), 'Text' => $oneMessage->getMessageText(), ); if (...) { // conditionally add more elements $newItem['Expired'] = $expiringDate; } // Push the final result into $ausgabe $ausgabe[] = $newItem; 会产生麻烦,因为当您想要在以后添加更多子元素时,您不知道引用新数组的键。你可以动态地找到它,但这对于没有任何好处来说太麻烦了。

答案 2 :(得分:0)

将您的代码编辑为:

    $i=0;
    foreach ($messageRepo as $oneMessage) {
    $calculatedTime = $oneMessage->getTimeToLive();
    $creationTime = $oneMessage->getCrdate();

    $calculatedTimes = $this->androidService->renderFormat($calculatedTime);
    $expiringDate = $creationTime + $calculatedTime;

    $ausgabe[$i] = array(
        'Time' => key($calculatedTimes),
        'Format' => current($calculatedTimes),
        'Title' => $oneMessage->getMessageTitle(),
        'Text' => $oneMessage->getMessageText(),
        );

    if (time() > $expiringDate) {
       $ausgabe[$i]['Expired'] = $expiringDate;

        } else {
        $ausgabe[$i]['Expired'] = $expiringDate;
        }
$i++;


}