PHP array summation, array intersection, content replacement and other operations_PHP tutorial

WBOY
Release: 2016-07-13 16:55:00
Original
774 people have browsed it

This tutorial mainly talks about intermediate data operations, including finding the sum and intersection of array elements, content replacement, and finding array content.

This tutorial mainly talks about intermediate data operations, including finding the sum and intersection of array elements, content replacement, and finding array content.
*/
$input=array("red","green","blue","yellow"); //Define the original array
array_splice($input,2); //Perform removal operation
print_r($input); //Output the processed result
$input=array("red","green","blue","yellow"); //Define the original array
array_splice($input,1,-1);                       //Perform the removal operation and specify the length
print_r($input); //Output the processed result
$input=array("red","green","blue","yellow"); //Define the original array
array_splice($input,1,count($input),"orange"); //Perform the removal operation and specify the length and content
print_r($input); //Output the processed result
$input=array("red","green","blue","yellow"); //Define the original array
array_splice($input,-1,1,array("black","maroon")); //Replacement content is an array
print_r($input); //Output the processed result
$input=array("red","green","blue","yellow"); //Define the original array
array_splice($input,3,0,"purple");
print_r($input); //Output the processed result

//
$sweet=array('a'=>'apple','b'=>'banana'); //Define the original array
$fruits=array('sweet'=>$sweet,'sour'=>'lemon'); //Define another array
function test_print($item,$key) //User-defined function
{
echo "$key holds $itemn"; //Output two parameters
}
array_walk_recursive($fruits,'test_print'); //Recursively call a custom function on array members

//Summing
$a=array(2,4,6,8); //Define the original array
echo "sum(a)=".array_sum($a)."n"; //Sum
$b=array("a"=>1.2,"b"=>2.3,"c"=>3.4); //Define the original array
echo "sum(b)=".array_sum($b)."n"; //Sum

//Array intersection
function strcasecmp($key1, $key2)
{
if($key1==$key2) //If the two parameters are equal
Return 0; //Return 0
else if($key1>$key2) //If the previous one is greater than the next one
return 1; //Return 1
else else //If the previous one is less than the next one
return -1; //Return -1
}
$a1=array("a"=>"green","b"=>"brown","c"=>"blue","red"); //Define array 1
$a2=array("a"=>"green","b"=>"brown","yellow","red"); //Define array 2
print_r(array_uintersect_uassoc($a1,$a2,"strcasecmp","function")); //Find the intersection of two arrays

//Array intersection
$a=array("a"=>"green","b"=>"brown","c"=>"blue","red"); //Define array 1
$b=array("a"=>"green","b"=>"brown","yellow","red"); //Define array 2
$result=array_uintersect($a,$b,"strcasecmp"); //Calculate array intersection
print_r($result);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631719.htmlTechArticleThis tutorial mainly talks about intermediate operations on data, such as finding the sum and intersection of array elements, replacing them, and finding the array content. This tutorial mainly talks about intermediate data operations, finding array elements...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!