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

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

WBOY
Release: 2016-06-06 20:33:49
Original
1431 people have browsed it

有两个相同的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>
Copy after login
Copy after login

回复内容:

有两个相同的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>
Copy after login
Copy after login

既然key一样,可以这样:

<code>php</code><code>while (list($key, $value) = each($array1)) {
    echo $value / $array2[$key];
}
</code>
Copy after login

也可以这样

<code>php</code><code>$keys = array_keys($array1);
foreach ($keys as $key) {
    echo $array1[$key] / $array2[$key];
}
</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template