Reputation: 581
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.
Upvotes: 2
Views: 2499
Reputation: 1192
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++;
}
Upvotes: 0
Reputation: 57326
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;
}
Upvotes: 3
Reputation: 437584
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.
Upvotes: 1