2

I would like to add a key=>value pair to an existing array depending on an if statement. But it keeps adding the key=>value pair as a new index.

Here is my code:

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);
            }   
    }

The dump says:

    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

But I want Expired => NULL as field in the original index and not as a new one.

2
  • 1
    You probably shouldn't use array_push() in the first place. Commented Jan 22, 2013 at 12:40
  • You probably shouldn't have different keys depending on whether it has expired or not! Commented Jan 22, 2013 at 12:50

3 Answers 3

3

You shouldn't use array_push in this case. Use simple assignment instead. As you don't know the index of the element that you are adding, you can create the new array, set all its values and then add it to the overall array. Something like this:

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! That works. :) I'll accept in a few minutes.
1

You are using array_push together with the $array[] notation, which does the same thing. The end result is creating a new element and then treating that as an array and putting another new element inside.

You should never have any need to use array_push directly. You should use something like this:

// 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;

Inserting the half-baked new array into $ausgabe gives trouble because when you want to add more sub-elements later you don't know the key that refers to the new array. You could find it out dynamically, but that's just too much trouble for no benefit.

Comments

0

Edit your code to:

    $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++;


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.