/    Sign up×
Community /Pin to ProfileBookmark

Can someone assist with this foreach loop?

All I am Trying to do is Loop thru my Array fo results and out each one seperate if there is more than 1 data point:

Trying to display each of tracking # as separate line:

[code] getIsOrderShipped($_order->getId()); // Is oorder shipped or not

“`
$items = $_order->getAllVisibleItems();
$trackingNumbers = [];

$trackNo = $adminHelper->getOrderTrackingNo($_order->getId());
$trackNos = explode(“,”, $trackNo);
if (count($trackNos)) {
for ($i=0; $i< count($trackNos); $i++) {
$trackingURL = $adminHelper->getTrackingUrl(strtolower($_order->getShippingDescription()), $trackNo);
$trackingNumbers[] = array(“track_no” => $trackNo, “url” => $trackingURL);
}
}

$trackingInfoInEmail = [];
$i = 0;
if (count($trackingNumbers) > 1) {
foreach($trackingNumbers as $trackingNumber) {
$i++;
$trackingInfoInEmail[] = “<a href='” . $trackingNumber[$i][‘url’] . “‘ target=’_blank’>” . $trackingNumber[$i][‘track_no’] . “</a>”;
return false;
}

}else{
$trackingInfoInEmail[] = “<a href='” . $trackingNumbers[][‘url’] . “‘ target=’_blank’>” . $trackingNumbers[][‘track_no’] . “</a>”;
}
$displayShippingMethod = $adminHelper->getTrackingTextInEmail($_order->getShippingDescription()); //Shipping Method Name

if (!$isOrderShipped) {
return false;
} else {
if (count($items) > 0) {
echo “You may receive multiple shipments and emails. The tracking number for this shipment is ” . $displayShippingMethod . ” #&nbsp;” . implode(‘,’, $trackingInfoInEmail);
} else {
echo “The tracking number for this shipment is ” . $displayShippingMethod . ” #&nbsp;” . implode(‘,’, $trackingInfoInEmail);
}
}
“`

[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 13.2018 — Why do you return false; in your foreach() loop? Do you only want to display the first one in the $trackingNumbers array? If you want them all, instead, you could just do this, without the separate else condition if there is only one result (I think, not knowing exactly what your data source and the arrays look like):
``php<i>
</i>if (count($trackingNumbers)) {
foreach($trackingNumbers as $trackingNumber) {
$trackingInfoInEmail[] = "&lt;a href='" . $trackingNumber['url'] . "' target='_blank'&gt;" . $trackingNumber['track_no'] . "&lt;/a&gt;";
}
}<i>
</i>
``
Copy linkTweet thisAlerts:
@NogDogSep 13.2018 — PS: Just a stylistic thing, but I would prefer to use "complex variable notation" in this case, as opposed to all that string concatenation -- but others may disagree. ?
``<i>
</i> $trackingInfoInEmail[] = "&lt;a href='{$trackingNumber['url']}' target='_blank'&gt;{$trackingNumber['track_no']}&lt;/a&gt;";<i>
</i>
``
×

Success!

Help @danjapro spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...