user3692430
user3692430

Reputation: 13

foreach loop with key in PHP

In the following foreach loop, how would I access the value of $url?

 foreach ( $this->sessions as $i => $url )
      curl_multi_add_handle( $mh, $this->sessions[$i] );
 …

I am trying to understand how that value for the url would be accessed for each particular cycle of the loop.

Upvotes: 0

Views: 70

Answers (1)

Barmar
Barmar

Reputation: 781068

You just use the variable:

foreach ( $this->sessions as $i => $url ) {
    curl_multi_add_handle( $mh, $url );
}

Also, get in the habit of always putting braces around the body of for, foreach, if, while, etc., even if it's just one line. It prevents difficult-to-find errors in the future.

Upvotes: 5

Related Questions