PHP:数组函数(1) 数组函数:1.需求:1)获取数组中的value.function getval($arr){ foreach($arr as $key$val){ $row[]$val; } return $row; }**?php $arrarray( item1linux, item2php, item3java, item4mysql, item5jquery ); // 数组遍历 foreach($arr as $key$val){ $row[]$val; } echo pre; print_r($row); echo /pre; ?结果是Array ( [0] linux [1] php [2] java [3] mysql [4] jquery )**?php $arrarray( item1linux, item2php, item3java, item4mysql, item5jquery ); // 数组遍历 function getval($arr){ foreach($arr as $key$val){ $row[]$val; } return $row; } $rowgetval($arr); echo pre; print_r($row); echo /pre; ?*结果是 *Array ( [0] linux [1] php [2] java [3] mysql [4] jquery )2)获取数组中的key.function getval($arr){ foreach($arr as $key$val){ $row[]$key; } return $row; }**?php $arrarray( item1linux, item2php, item3java, item4mysql, item5jquery ); // 数组遍历 function getval($arr){ foreach($arr as $key$val){ $row[]$key; } return $row; } $rowgetval($arr); echo pre; print_r($row); echo /pre; ?结果是Array ( [0] item1 [1] item2 [2] item3 [3] item4 [4] item5 )3)把数组中的key和value对调.function getval($arr){ foreach($arr as $key$val){ $row[$val]$key; } return $row; }**?php $arrarray( item1linux, item2php, item3java, item4mysql, item5jquery ); // 数组遍历 function getval($arr){ foreach($arr as $key$val){ $row[$val]$key; } return $row; } $rowgetval($arr); echo pre; print_r($row); echo /pre; ?结果是Array ( [linux] item1 [php] item2 [java] item3 [mysql] item4 [jquery] item5 )4)求数组中所有值之和.function getval($arr){ foreach($arr as $key$val){ $tot$val; } return $tot; }**?php $arrarray(1,2,3,58,6,6,485,96,655); // 数组遍历 function getval($arr){ foreach($arr as $key$val){ $tot$val; } return $tot; } $rowgetval($arr); echo pre; print_r($row); echo /pre; ?结果是13125)求数组中所有值的乘积.function getval($arr){ $tot1; foreach($arr as $key$val){ $tot*$val; } return $tot; }**?php $arrarray(1,2,3,58,6,6,485,96,655); // 数组遍历 function getval($arr){ $tot1; foreach($arr as $key$val){ $tot*$val; } return $tot; } $rowgetval($arr); echo pre; print_r($row); echo /pre; ?*结果是 *3820639104006)判断一个值在不在数组中.function getval($str,$arr){ foreach($arr as $key$val){ if($val$str){ return true; } } }**?php $arrarray( item1linux, item2js, item3java, item4html, item5css, item6python, item7c, item8javascript, ); // 数组遍历 function getval($str,$arr){ foreach($arr as $key$val){ if($val$str){ return true; } } } $rowgetval(js,$arr); var_dump($row); ?结果是bool(true)7)元素个数.function getval($str,$arr){ foreach($arr as $key$val){ $tot; } return $tot; }**?php $arrarray( item1linux, item2js, item3java, item4html, ); $arr2array( item5css, item6python, item7c, item8javascript, ); // 数组遍历 function getval($arr){ foreach($arr as $key$val){ $tot; } return $tot; } $rowgetval($arr); echopre; print_r($row); echo/pre; ?*结果是 *48)数组值合并.function getval($arr,$arr2){ foreach($arr as $key$val){ $row[$key]$val; } foreach($arr2 as $key2$val2){ $row[$key2]$val2; } return $row; }**?php $arrarray( item1linux, item2js, item3java, item4html, ); $arr2array( item5css, item6python, item7c, item8javascript, ); // 数组遍历 function getval($arr,$arr2){ foreach($arr as $key$val){ $row[$key]$val; } foreach($arr2 as $key2$val2){ $row[$key2]$val2; } return $row; } $rowgetval($arr,$arr2); echopre; print_r($row); echo/pre; ?结果是Array ( [item1] linux [item2] js [item3] java [item4] html [item5] css [item6] python [item7] c [item8] javascript )9)数组过滤.function getval($arr){ foreach($arr as $val){ if($val%21){ $row[]$val; } } return $row; }**?php $arrarray(0,1,2,3,4,5,6,7,8,9,10); // 数组遍历 function getval($arr){ foreach($arr as $val){ if($val%21){ $row[]$val; } } return $row; } $rowgetval($arr); echopre; print_r($row); echo/pre; ?**Array ( [0] 1 [1] 3 [2] 5 [3] 7 [4] 9 )10)数组键值合并.function getval($arr,$arr2){ for($i0;$i2;$i){ $row[$arr[$i]]$arr2[$i]; } return $row; }**?php $arrarray( linux, js, java, html, ); $arr2array( css, python, c, javascript, ); // 数组遍历 function getval($arr,$arr2){ for($i0;$i4;$i){ $row[$arr[$i]]$arr2[$i]; } return $row; } $rowgetval($arr,$arr2); echopre; print_r($row); echo/pre; ?结果是Array ( [linux] css [js] python [java] c [html] javascript**