Ask PHP algorithm, two-dimensional array to take out one-dimensional array of the same ID as several new two-dimensional arrays.

Source: Internet
Author: User
PHP two-dimensional array algorithm

Little brother just learned PHP soon, encountered a problem, ask the heroes, thank you!

The two-dimensional array is as follows;
$arr = Array (
Array (' ID ' =>100, ' value ' =>100),
Array (' ID ' =>100, ' value ' =>130,
Array (' ID ' =>100, ' value ' =>140),
Array (' ID ' =>100, ' value ' =>150),
Array (' ID ' =>101, ' value ' =>1600),
Array (' ID ' =>101, ' value ' =>100),
Array (' ID ' =>102, ' value ' =>100),
Array (' ID ' =>102, ' value ' =>100),
Array (' ID ' =>102, ' value ' =>100),
Array (' ID ' =>103, ' value ' =>100),
Array (' ID ' =>103, ' value ' =>100),
Array (' ID ' =>104, ' value ' =>100),
Array (' ID ' =>104, ' value ' =>100),
Array (' ID ' =>104, ' value ' =>100),
Array (' ID ' =>105, ' value ' =>100)
);


How to split into the following 5 two-dimensional arrays:
ARR1 (
Array (' ID ' =>100, ' value ' =>100),
Array (' ID ' =>100, ' value ' =>130,
Array (' ID ' =>100, ' value ' =>140),
Array (' ID ' =>100, ' value ' =>150)
);

ARR2 (
Array (' ID ' =>101, ' value ' =>1600),
Array (' ID ' =>101, ' value ' =>100)
);

ARR3 (
Array (' ID ' =>102, ' value ' =>100),
Array (' ID ' =>102, ' value ' =>100),
Array (' ID ' =>102, ' value ' =>100)
);

ARR4 (
Array (' ID ' =>103, ' value ' =>100),
Array (' ID ' =>103, ' value ' =>100)

);

ARR5 (
Array (' ID ' =>104, ' value ' =>100),
Array (' ID ' =>104, ' value ' =>100),
Array (' ID ' =>104, ' value ' =>100),
Array (' ID ' =>105, ' value ' =>100)
);

ARR5 (
Array (' ID ' =>105, ' value ' =>100)
);


Reply to discussion (solution)

ARR5 (
Array (' ID ' =>104, ' value ' =>100),
Array (' ID ' =>104, ' value ' =>100),
Array (' ID ' =>104, ' value ' =>100),
);

ARR6 (
Array (' ID ' =>105, ' value ' =>100)
);

??


Is it 6 arrays or 5 arrays? Two ARR5 the data is not the same?

Really don't understand ~ ~ ~ can you tell me the law? I didn't see the rules of the 6 arrays below you .... In particular, the two arr5 inside of the thing directly to the whole circle of people ...

6, the last ARR5 should be: ARR6

ARR6 (
Array (' ID ' =>105, ' value ' =>100)
);

Sorry, copy error.

Is the same ID to put in a two-dimensional array,
The above array has 6 kinds of id:100,101,102,103,104,105, which are to be put into 6 different two-dimensional arrays respectively.

$arr = array (' ID ' =>100, ' value ' =>100), array (' ID ' =>100, ' value ' =>130), array (' ID ' =>100, ' value ' = >140), array (' ID ' =>100, ' value ' =>150), array (' ID ' =>101, ' value ' =>1600), array (' ID ' =>101, ' value ' = >100), array (' ID ' =>102, ' value ' =>100), array (' ID ' =>102, ' value ' =>100), array (' ID ' =>102, ' value ' = >100), array (' ID ' =>103, ' value ' =>100), array (' ID ' =>103, ' value ' =>100), array (' ID ' =>104, ' value ' = >100), array (' ID ' =>104, ' value ' =>100), array (' ID ' =>104, ' value ' =>100), array (' ID ' =>105, ' value ' = >100)); foreach ($arr as $key + $val) {$newarr [$val [' ID ']][]=array (' id ' = ' + ' $val [' id '], ' value ' = * = $val [' Value ']);} $arr 1= $newarr [' n ']; $arr 2= $newarr [' 101 ']; $arr 3= $newarr [' 102 ']; $arr 4= $newarr [' 103 ']; $arr 5= $newarr [' 104 ']; $arr 6 = $newarr [' 105 '];

$arr = Array (  ' id ' =>100, ' value ' =>100),  array (' ID ' =>100, ' value ' =>130),  array (' id ' = >100, ' value ' =>140),  array (' ID ' =>100, ' value ' =>150),  array (' ID ' =>101, ' value ' =>1600), Array (  ' id ' =>101, ' value ' =>100),  array (' ID ' =>102, ' value ' =>100),  array (' ID ' =>102, ' Value ' =>100 ', array (  ' id ' =>102, ' value ' =>100),  array (' ID ' =>103, ' value ' =>100),  Array ( ' id ' =>103, ' value ' =>100),  array (' ID ' =>104, ' value ' =>100),  array (' ID ' =>104, ' value ' = =  Array (' ID ' =>104, ' value ' =>100),  array (' ID ' =>105, ' value ' =>100)); foreach ($arr as $v) $ res[$v [' id ']][] = $v;//print_r ($res);//You can directly use this array//You can also import the variable table after accessing Extract (Array_values ($res), Extr_prefix_all, ' arr ') ;//such as Print_r ($arr _3);

Thank you very much AHUI_LCM and xuzuning, so simple conversion I did not think out, really ashamed.
Ask the next 2 and you heroes, how can I quickly improve the operation of the PHP array. are always affected by arrays in other languages.
Thank you! Score I'm still going to give the first brother AHUI_LCM to help me. Brother Xuzuning to answer the more perfect point, also very grateful to xuzuning.

The code above//This code is run directly under the $arr = array (' id ' = ' = ', ' value ' = ' + '), array (' id ' = +, ' value ' = '), a Rray (' id ' = +, ' value ' = + +), array (' id ' = ' = ', ' value ' = '), array (' id ' = = 101, ' value ' =&gt ; ), array (' id ' = = 101, ' value ' = +), array (' id ' = = 102, ' value ' = +), array (' id ' = = 102, ' VA Lue ' + + ', array (' id ' = = 102, ' value ' = +), array (' id ' = = 103, ' value ' = +), array (' ID ' =&gt ; 103, ' value ' = +), array (' id ' = = 104, ' value ' = +), array (' id ' = = 104, ' value ' = +), Array (' id ' = = 104, ' value ' = ' + ', array (' id ' = ' = ' value ' = ') '), foreach ($arr as $v) $res [$v [' id ']][] = $v ; My_debug ($res);//You can directly use this array//You can also import the variable table after accessing Extract (Array_values ($res), Extr_prefix_all, ' arr '); My_debug ($arr _0); My_debug ($arr _1); My_debug ($arr _2); My_debug ($arr _3); function my_debug ($v = null, $var _name = null, $ method = ' Dump ', $backtrace = False) {$display _none = ";" if (Defined (' FRONTPAGE ')) {$display _none = "display:none;";} Print (""); $call _stack = Debug_backtrace ();p rintf (' file:%s,%s (), line:%s
' @ $call _stack [0] [' file '], @ $call _stack [1] [' function '], @ $call _stack [0] [' line ']), if (! $var _name) {//Take $v in source code variable name . $v _line = file ($call _stack [0] [' file ']) $f _line = $v _line [$call _stack [0] [' line ']-1]; $match = Array ();p Reg_match (' #my_debug. *[(].*# ', $f _line, $match); if (count ($match)) {$var _name = $match [0];}} if ($var _name) {printf (' Debug:%s ', $var _name);} Xdebug_var_dump ($v);p rint ('
if ($method = = ' dump ') {var_dump ($v);} else {print_r ($v);} Print ("
"), if ($backtrace) {foreach (Debug_backtrace () as $v) {echo ($v [' file '].": ". $v [' line ']. "," . $v [' class ']. "," . $v [' function ']. "() \ n
\ n ");}} Print (');p rint ("\ n");}

My_debug ($arr _0); My_debug ($arr _1); My_debug ($arr _2); My_debug ($arr _3); My_debug ($arr _4); My_debug ($arr _5);/* Instructions to print out these are all out! ... my_debug is the effect function of my print Group!!!!! Very practical, you can also locate the line ... * *
  • 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.