Home >Backend Development >PHP Tutorial >PHP两个数组除以计算如何写?
有两个相同的key值,我想两个数组除以计算,循环下如何写呢?
<code>array1 =array([id]=>11 [id]=> 22[id]=> 33[id]=> 44[id]=> 88 [id]=> 99[id]=> 88[id]=> 11[id]=> 22); array2= array([id]=>11 [id]=> 22[id]=> 33[id]=> 44[id]=> 88 [id]=> 99[id]=> 88[id]=> 11[id]=> 22); foreach( ??? as $v){ echo $jieguo= (array1=$v['id'] / array2=$v['id'] ); } </code>
有两个相同的key值,我想两个数组除以计算,循环下如何写呢?
<code>array1 =array([id]=>11 [id]=> 22[id]=> 33[id]=> 44[id]=> 88 [id]=> 99[id]=> 88[id]=> 11[id]=> 22); array2= array([id]=>11 [id]=> 22[id]=> 33[id]=> 44[id]=> 88 [id]=> 99[id]=> 88[id]=> 11[id]=> 22); foreach( ??? as $v){ echo $jieguo= (array1=$v['id'] / array2=$v['id'] ); } </code>
既然key一样,可以这样:
<code>php</code><code>while (list($key, $value) = each($array1)) { echo $value / $array2[$key]; } </code>
也可以这样
<code>php</code><code>$keys = array_keys($array1); foreach ($keys as $key) { echo $array1[$key] / $array2[$key]; } </code>