PHP number combinations and re-merging _php tutorials based on classification

Source: Internet
Author: User
In PHP if it is a simple number combination and there are many methods, but I asked today to be based on the array ID and then re-merge the array, is to be based on the classification to operate.

The simplest array merge we just use Array_merge to

Array_merge () Merges the cells of two or more arrays, and the values in one array are appended to the previous array. Returns an array as the result.

When the array key name is a numeric key name, the two arrays to be merged have the same name as the number key, using Array_merge () will not overwrite the original value, while using "+" to merge the array will return the first occurrence as the final result, and the values of the following array with the same key name "Discard" (note: Instead of overwriting, it retains the first value that appears). Example:

code as follows copy code

$array 1 = array (1=> ' 0 ');
$array 2 = Array (1=> "data"); 
$result 1 = $array 2 + $array 1;/* result is the value of $array2 */
Print_r ($result);
  $result = $array 1 + $array 2;/* result is $array1 value */
Print_r ($result);
  $result 3 = Array_merge ($array 2, $array 1);/* The result is a value of $array2 and $array1, and the key name is reassigned */
Print_r ($result 3);
$result 4 = Array_merge ($array 1, $array 2);/* The result is a value of $array1 and $array2, and the key name is reassigned */
Print_r ($result 4);

The

Output 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 the key name, but Array_merge () overrides the previous value of the same key name.
Example:

copy code


$array 1 = array (' asd ' = ' 0 ');
$array 2 = Array (' ASD ' = ' data '); 
$result 1 = $array 2 + $array 1;/* result is the value of $array2 */
Print_r ($result);
  $result = $array 1 + $array 2;/* result is $array1 value */
Print_r ($result);
  $result 3 = Array_merge ($array 2, $array 1);/* The result is $array1*/
Print_r ($result 3);
$result 4 = Array_merge ($array 1, $array 2);/* The result is $array2*/
Print_r ($result 4);

The


output is:

Array ([ASD] = data)
Array ([ASD] + 0)
Array ([ASD] + 0)
Array ([ASD] => ; Data)

With so many crap, here's what I'm going to tell you guys.

Cases.

Array reorganization based on categorical fields

The code is as follows Copy Code


Arrays that need 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);
Arrays are reorganized according to classification
$newArray =array ();
foreach ($array as $val) {
$newArray [$val [' CategoryID ']][]= $val;
}

Delete original array free space
$array =null;
Unset ($array);

Print_r ($newArray);

?>

http://www.bkjia.com/PHPjc/633061.html www.bkjia.com true http://www.bkjia.com/PHPjc/633061.html techarticle in PHP If it is a simple number combination and there are many methods, but I asked today to be based on the array ID and then re-merge the array, is to be based on the classification to operate. The simplest array of merges I ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.