There are many methods for simple array merging in php, but my requirement today is to re-merge the arrays based on the array ID, and to operate based on classification.
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:
The code is as follows |
Copy code |
代码如下 |
复制代码 |
$array1 = array(1=>'0');
$array2 = array(1=> "data");
$result1 = $array2 + $array1;/*结果为$array2的值*/
print_r($result);
$result = $array1 + $array2 ;/*结果为$array1的值*/
print_r($result);
$result3 = array_merge($array2,$array1);/*结果为$array2和$array1的值,键名被重新分配*/
print_r($result3);
$result4 = array_merge($array1,$array2);/*结果为$array1和$array2的值,键名被重新分配*/
print_r($result4);
输出结果为:
Array ( [1] => data )
Array ( [1] => 0 )
Array (
[0] => data
[1] => 0
)
Array
(
[0] => 0
[1] => data
)
|
$array1 = array(1=>'0');
$array2 = array(1=> "data");
$result1 = $array2 + $array1;/*The result is the value of $array2*/
print_r($result);
代码如下 |
复制代码 |
$array1 = array('asd'=>'0');
$array2 = array('asd' => "data");
$result1 = $array2 + $array1;/*结果为$array2的值*/
print_r($result);
$result = $array1 + $array2 ;/*结果为$array1的值*/
print_r($result);
$result3 = array_merge($array2,$array1);/*结果为$array1*/
print_r($result3);
$result4 = array_merge($array1,$array2);/*结果为$array2*/
print_r($result4);
输出结果为:
Array ( [asd] => data )
Array ( [asd] => 0 )
Array ( [asd] => 0 )
Array ( [asd] => data )
|
$result = $array1 + $array2 ;/*The result is the value of $array1*/
print_r($result);
$result3 = array_merge($array2,$array1);/*The result is the value of $array2 and $array1, and the key name is reassigned*/
print_r($result3);
$result4 = array_merge($array1,$array2);/*The result is the value of $array1 and $array2, and the key name is reassigned*/
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 of the same key name.
Example:
The code is as follows |
Copy code |
$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
Example.
Array reorganization based on classification fields
The code is as follows
代码如下 |
复制代码 |
//需要重组的数组
$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);
//数组根据分类进行重组
$newArray=array();
foreach($array as $val){
$newArray[$val['CategoryID']][]=$val;
}
//删除原始数组释放空间
$array=null;
unset($array);
print_r($newArray);
?>
|
|
Copy code |
|
//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/633061.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/633061.htmlThere are many methods for simple array merging in php, but my requirement today is to re-merge it based on the array ID. Merging arrays is based on classification. The simplest array merge I have...