简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM