Skip to content

Instantly share code, notes, and snippets.

@tps2015gh
Last active January 24, 2017 07:04
Show Gist options
  • Save tps2015gh/2612fe1389fa86e5be8a2f97b02bf06c to your computer and use it in GitHub Desktop.
Save tps2015gh/2612fe1389fa86e5be8a2f97b02bf06c to your computer and use it in GitHub Desktop.
<?php
$array1 = ['1','2','3'];
$array2 = ['1','3','5'];
function intersec($array1, $array2){
$ar_ret = array_filter( $array1 ,
function ($e) use ($array2){ /* closure , anonymous function*/
return in_array($e,$array2) ;
});
return $ar_ret;
}
// TET
$ar_ret = intersec($array1,$array2);
print_r($ar_ret );
/*
Return Print
Array ( [0] => 1 [2] => 3 )
*/
<?php
$array1 = ['1','2','3'];
$array2 = ['1','4','5'];
$ret = array_reduce($array1, function($total,$e){
return $total+ $e ;
},0);
echo $ret ;
$ret = array_reduce($array1, function($total,$e){
return $total+ $e ;
},$ret);
echo " / " ;
echo $ret ;
/* NOTE: Print '6 / 12' */
<?php
$array1 = ['1','2','3','4'];
$array2 = ['1','3','5','4'];
function get_count_pos($array1,$array2){
$idx = -1 ;
return array_reduce($array1, function($total,$a1val) use($array2 , &$idx ){
$idx ++ ; /*start from index=0 */
return $total + ($a1val==$array2[$idx] ? 1 :0) ;
}, 0 /*init as 0 */ );
}
// TET
$ar_ret = get_count_pos($array1,$array2);
print_r($ar_ret );
/* Return print '2' */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment