数组函数:
1.需求:
1)获取数组中的value.
function getval($arr){ foreach($arr as $key=>$val){ $row[]=$val; } return $row; }**
<?php $arr=array( 'item1'=>'linux', 'item2'=>'php', 'item3'=>'java', 'item4'=>'mysql', 'item5'=>'jquery' ); // 数组遍历 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 $arr=array( 'item1'=>'linux', 'item2'=>'php', 'item3'=>'java', 'item4'=>'mysql', 'item5'=>'jquery' ); // 数组遍历 function getval($arr){ foreach($arr as $key=>$val){ $row[]=$val; } return $row; } $row=getval($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 $arr=array( 'item1'=>'linux', 'item2'=>'php', 'item3'=>'java', 'item4'=>'mysql', 'item5'=>'jquery' ); // 数组遍历 function getval($arr){ foreach($arr as $key=>$val){ $row[]=$key; } return $row; } $row=getval($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 $arr=array( 'item1'=>'linux', 'item2'=>'php', 'item3'=>'java', 'item4'=>'mysql', 'item5'=>'jquery' ); // 数组遍历 function getval($arr){ foreach($arr as $key=>$val){ $row[$val]=$key; } return $row; } $row=getval($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 $arr=array(1,2,3,58,6,6,485,96,655); // 数组遍历 function getval($arr){ foreach($arr as $key=>$val){ $tot+=$val; } return $tot; } $row=getval($arr); echo '<pre>'; print_r($row); echo '</pre>'; ?>结果是
13125)求数组中所有值的乘积.
function getval($arr){ $tot=1; foreach($arr as $key=>$val){ $tot*=$val; } return $tot; }**
<?php $arr=array(1,2,3,58,6,6,485,96,655); // 数组遍历 function getval($arr){ $tot=1; foreach($arr as $key=>$val){ $tot*=$val; } return $tot; } $row=getval($arr); echo '<pre>'; print_r($row); echo '</pre>'; ?>*结果是 *
3820639104006)判断一个值在不在数组中.
function getval($str,$arr){ foreach($arr as $key=>$val){ if($val==$str){ return true; } } }**
<?php $arr=array( 'item1'=>'linux', 'item2'=>'js', 'item3'=>'java', 'item4'=>'html', 'item5'=>'css', 'item6'=>'python', 'item7'=>'c', 'item8'=>'javascript', ); // 数组遍历 function getval($str,$arr){ foreach($arr as $key=>$val){ if($val==$str){ return true; } } } $row=getval('js',$arr); var_dump($row); ?>结果是
bool(true)7)元素个数.
function getval($str,$arr){ foreach($arr as $key=>$val){ $tot++; } return $tot; }**
<?php $arr=array( 'item1'=>'linux', 'item2'=>'js', 'item3'=>'java', 'item4'=>'html', ); $arr2=array( 'item5'=>'css', 'item6'=>'python', 'item7'=>'c', 'item8'=>'javascript', ); // 数组遍历 function getval($arr){ foreach($arr as $key=>$val){ $tot++; } return $tot; } $row=getval($arr); echo'<pre>'; 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 $arr=array( 'item1'=>'linux', 'item2'=>'js', 'item3'=>'java', 'item4'=>'html', ); $arr2=array( 'item5'=>'css', 'item6'=>'python', 'item7'=>'c', 'item8'=>'javascript', ); // 数组遍历 function getval($arr,$arr2){ foreach($arr as $key=>$val){ $row[$key]=$val; } foreach($arr2 as $key2=>$val2){ $row[$key2]=$val2; } return $row; } $row=getval($arr,$arr2); echo'<pre>'; 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%2==1){ $row[]=$val; } } return $row; }**
<?php $arr=array(0,1,2,3,4,5,6,7,8,9,10); // 数组遍历 function getval($arr){ foreach($arr as $val){ if($val%2==1){ $row[]=$val; } } return $row; } $row=getval($arr); echo'<pre>'; print_r($row); echo'</pre>'; ?>**
Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 9 )10)数组键值合并.
function getval($arr,$arr2){ for($i=0;$i<2;$i++){ $row[$arr[$i]]=$arr2[$i]; } return $row; }**
<?php $arr=array( 'linux', 'js', 'java', 'html', ); $arr2=array( 'css', 'python', 'c', 'javascript', ); // 数组遍历 function getval($arr,$arr2){ for($i=0;$i<4;$i++){ $row[$arr[$i]]=$arr2[$i]; } return $row; } $row=getval($arr,$arr2); echo'<pre>'; print_r($row); echo'</pre>'; ?>结果是
Array ( [linux] => css [js] => python [java] => c [html] => javascript**