Asked  7 Months ago    Answers:  5   Viewed   41 times

I am writing a SQL query creator using some parameters. In Java, it's very easy to detect the last element of an array from inside the for loop by just checking the current array position with the array length.

for(int i=0; i< arr.length;i++){
     boolean isLastElem = i== (arr.length -1) ? true : false;        
}

In PHP they have non-integer indexes to access arrays. So you must iterate over an array using a foreach loop. This becomes problematic when you need to take some decision (in my case to append or/and parameter while building query).

I am sure there must be some standard way of doing this.

How do you solve this in PHP?

 Answers

70

It sounds like you want something like this:

$numItems = count($arr);
$i = 0;
foreach($arr as $key=>$value) {
  if(++$i === $numItems) {
    echo "last index!";
  }
}    

That being said, you don't -have- to iterate over an "array" using foreach in php.

Wednesday, March 31, 2021
 
Skipper
answered 7 Months ago
20

In PHP 7, if you iterate an array by value, the copy will be done lazily, only when and if the array is actually modified.

If you iterate an array by reference instead, a separation will be performed at the start of the loop. If the array is currently used in more than one place, this separation will lead to a copy.

Furthermore iterating by reference means that a) the array has to be wrapped into a reference and b) each element has to be wrapped in a reference as well. Creating a reference wrapper is an expensive operation, because it requires allocation.

Additionally iteration by reference requires us to use a modification-safe iteration mechanism. This works by registering the iterator with the array and checking for potentially affected iterators in various array modification operations.

So no, iterating by reference is certainly not an optimization, it's a de-optimization. Using references usually is.

Wednesday, March 31, 2021
 
ariel
answered 7 Months ago
81

Instead of using BCC, you'd do something like:

... initialize PHP mailer ...
... set options common to ALL emails ...
foreach( ... loop over all recipients ...) {
    $mail->ClearAddresses(); // remove previous email addresses
    $mail->AddAddress($new_recipient_here);
    $mail->Body = <<<EOL
... customized html here
<img src="http://domain.com/email_test/email_beacon.php?campaign={$intCampaignID}&email={$address}" alt="" />
... more html here
EOL;

    $mail->send();
}
Wednesday, March 31, 2021
 
scessor
answered 7 Months ago
38

EDIT

The solution you want is this:

$user = current( User::findByEmail( $_SESSION['email'] ) );

Note that using current() with functions always returns the first element, because the array was never assigned to a variable, and does not have a point. This is NOT the case if the function returns an array by reference.

Wednesday, March 31, 2021
 
Dail
answered 7 Months ago
92

The array has a Length property that will give you the length of the array. Since the array indices are zero-based, the last item will be at Length - 1.

string[] items = GetAllItems();
string lastItem = items[items.Length - 1];
int arrayLength = array.Length;

When declaring an array in C#, the number you give is the length of the array:

string[] items = new string[5]; // five items, index ranging from 0 to 4.
Thursday, July 29, 2021
 
NaeiKinDus
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :