foreach statement can also get the key name of the array, as follows:
two , echo() function The function of each() is to split the key-value pairs of the current element of the array into a new array, and use the next element as the current element. For example, the 'Robert'=>'Bob' key-value pair in Array(...,'Robert'=>'Bob',...) is split into Array([1]=>'Bob',[value] =>'Bob',[0]=>'Robert',[key]=>'Robert') array, split into two sets (a total of four key-value pairs) to return, the serial number pairs of 0 and 1, key and value name-value pairs, just use one of them. Example:
output result: Tires-100 Oil-10 Spank Plugs-4 Three, the list() function traverses the array The list() function can be used to decompose an array into a series of values. list() is often used together with each(). But list() can also be used without each(), for example: list($key, $value) = explode(":", $v); list($key, $value) = each($array); //$key, $value can be named arbitrarily. This sentence assigns the current elements at positions 0 and 1 of the array returned by each to the $key and $value variables. example:
Output result: Tires-100 Oil-10 Spank Plugs-4 Another implementation method:
|