Home >Backend Development >PHP Tutorial >PHP两个数组除以计算如何写?

PHP两个数组除以计算如何写?

WBOY
WBOYOriginal
2016-06-06 20:33:491423browse

有两个相同的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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn