PHP array Foreach,join

Source: Internet
Author: User
One, the use of foreach
For example: $arr = Array ("One", "one", "three");
Reset ($arr);//The inner pointer of the array points to the first element and returns the value of the element. If it fails, it returns FALSE.

Array Loop Output 1
foreach ($arr as $value) {
echo ' Value = '. $value. ' <br/> ';
}
Array Loop Output 2
foreach ($arr as $key = = $value) {
echo "Key: $key; Value: $value <br/>\n ";
}

Two, join combines array elements into a single string
$in = Join ("', '", $arr);


Third, foreach related exception resolution
1, Issue warning:invalid argument supplied for foreach () in perfect solution

Solution: Determine the variable in foreach with Is_array () or force it into an array
For example

PHP code

function Getpartsnum ($jsonStr) {      $total = 0;      if ($jsonStr && ($jsonStr! = ' NAN ') && ($jsonStr! = ' [] ')) {          $detail = Json_decode ($JSONSTR);          if (Is_array ($detail)) {//or foreach ((array) $detail as $d)              foreach ($detail as $d) {                  $total + = $d->num;              }          }      }      return $total;  }
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.