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;
}