Detailed explanation of method examples of php merging arrays according to classification_PHP tutorial

WBOY
Release: 2016-07-13 10:25:26
Original
674 people have browsed it

For the simplest array merging, we only need to use array_merge
array_merge() merges the cells of two or more arrays, and the values ​​in one array are appended to the previous array. Returns the resulting array.
When the array key name is a numeric key name, and the two arrays to be merged have numeric KEYs with the same name, using array_merge() will not overwrite the original value, while using "+" to merge the arrays will replace the first The value that appears is returned as the final result, and those values ​​in subsequent arrays with the same key name are "discarded" (note: the value that appears first is not overwritten but retained). Example:

Copy code The code is as follows:

$array1 = array(1=>'0');
$array2 = array(1=> "data");
$result1 = $array2 + $array1;/*The result is the value of $array2*/
print_r($result);
$ result = $array1 + $array2 ;/*The result is the value of $array1*/
  print_r($result);
  $result3 = array_merge($array2,$array1);/*The result is $array2 and $ The value of array1, the key name is reassigned*/
​​​​​print_r($result3); Re-allocate*/
print_r($result4);

The output result is:
Array ( [1] => data )
Array ( [1] => 0 )
Array (
[0] => data
[1] => 0
)
Array
(
[0] => 0
[1] => data
)

2. When the same array key name is a character, the "+" operator is the same as when the key name is a number, but array_merge() will Overwrite the previous value with the same key name.
Example:

Copy code The code is as follows:
$array1 = array('asd'=>'0 ');
$array2 = array('asd' => "data");
$result1 = $array2 + $array1;/*The result is the value of $array2*/
print_r($ result);
$result = $array1 + $array2 ;/*The result is the value of $array1*/
print_r($result);
$result3 = array_merge($array2,$array1);/ *The result is $array1*/
print_r($result3);
$result4 = array_merge($array1,$array2);/*The result is $array2*/
print_r($result4);

The output result is:
Array ( [asd] => data )
Array ( [asd] => 0 )
Array ( [asd] => 0 )
Array ( [asd] => data )

After talking so much, here is what I want to introduce to my friends
Array reorganization based on classification fields

Copy code The code is as follows:
//Array that needs to be reorganized
$arrar=array();
$array[]=array('ItemID' => 110126866896,'CategoryID'=>111);
$array[]=array('ItemID' => 120126866896,'CategoryID'=> 112);
$array[]=array('ItemID' => 130126866896,'CategoryID'=>113);
$array[]=array('ItemID' => 140126866896,'CategoryID '=>114);
$array[]=array('ItemID' => 150126866896,'CategoryID'=>115);
$array[]=array('ItemID' => 160126866896,'CategoryID'=>116);
$array[]=array('ItemID' => 170126866896,'CategoryID'=>117);
$array[]=array('ItemID ' => 118126866896,'CategoryID'=>111);
$array[]=array('ItemID' => 121126866896,'CategoryID'=>112);
$array[]= array('ItemID' => 132126866896,'CategoryID'=>113);
$array[]=array('ItemID' => 143126866896,'CategoryID'=>114);
$ array[]=array('ItemID' => 154126866896,'CategoryID'=>115);
$array[]=array('ItemID' => 165126866896,'CategoryID'=>116);
$array[]=array('ItemID' => 176126866896,'CategoryID'=>117);
//The array is reorganized according to classification
$newArray=array();
foreach($array as $val){
$newArray[$val['CategoryID']][]=$val;
}

//Delete the original array to free up space
$ array=null;
unset($array);

print_r($newArray);

?>

http://www.bkjia.com/PHPjc/825097.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825097.htmlTechArticleThe simplest array merging, we only need to use array_merge array_merge() to merge the cells of two or more arrays Together, the values ​​in one array are appended to the previous array. Return...
Related labels:
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!