PHP based on the classification of the array of methods to explain _php example

Source: Internet
Author: User
The simplest combination of arrays 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, when the two arrays to be merged have the same number key in the same name, using Array_merge () does not overwrite the original value, and using the "+" merge array will return the first occurrence as the final result, and those values with the same key name as the following array "Discard" Off (note: Instead of overwriting, keep the first occurrence of the value). Example:


Copy Code code as follows:

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



output results are:


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 is a number, but Array_merge () overrides the previous value of the same key name.


Example:


Copy Code code as follows:

$array 1 = Array (' ASD ' => ' 0 ');
$array 2 = Array (' ASD ' => "data");
$result 1 = $array 2 + $array 1;/* result is $array2 value * *
Print_r ($result);
$result = $array 1 + $array 2;/* Result $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);



output results are:


Array ([ASD] => data)


Array ([ASD] => 0)


Array ([ASD] => 0)


Array ([ASD] => data)





told so many crap, the following is the
I want to introduce to you friends

array reorganization based on the category field


 
Copy Code code as follows:

<?php
//need to reorganize the array
$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 ' => 1651268668, ' CategoryID ' =>116);
$array []=array (' ItemID ' => 176126866896, ' CategoryID ' =>117); The
//array is reorganized according to the classification
$newArray =array ();
foreach ($array as $val) {
    $newArray [$val [' CategoryID ']][]= $val;
}

//delete original array free space
$array =null;
Unset ($array);

Print_r ($newArray);

?>
 

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.