1

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

这是我的代码:

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作为原始索引中的字段而不是新索引。

4

3 回答 3

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;
}
于 2013-01-22T12:45:08.297 回答
1

您与符号array_push 一起使用,它做同样的事情。$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;

将半生不熟的新数组插入$ausgabe会带来麻烦,因为当您以后想要添加更多子元素时,您不知道引用新数组的键。你可以动态地找到它,但这太麻烦了,没有任何好处。

于 2013-01-22T12:43:07.200 回答
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++;


}
于 2013-01-22T12:43:37.433 回答