Php array filtering problem?

Source: Internet
Author: User
How can I filter an array like this into {code...}? How can I filter an array like this?

["Q"] => array(4) {        [0] => array(11) {          ["id"] => string(1) "1",["k"] => string(1) "Q"        }         [1] => array(11) {          ["id"] => string(1) "2",["k"] => string(1) "H"        }        [2] => array(11) {          ["id"] => string(1) "3", ["k"] => string(1) "V"        }         [3] => array(11) {          ["id"] => string(1) "4",  ["k"] => string(1) "V"        }}["V"] => array(4) {        [0] => array(11) {          ["id"] => string(1) "1",["k"] => string(1) "Q"        }         [1] => array(11) {          ["id"] => string(1) "2",["k"] => string(1) "H"        }        [2] => array(11) {          ["id"] => string(1) "3", ["k"] => string(1) "V"        }         [3] => array(11) {          ["id"] => string(1) "4",  ["k"] => string(1) "V"        }}["H"] => array(4) {        [0] => array(11) {          ["id"] => string(1) "1",["k"] => string(1) "Q"        }         [1] => array(11) {          ["id"] => string(1) "2",["k"] => string(1) "H"        }        [2] => array(11) {          ["id"] => string(1) "3", ["k"] => string(1) "V"        }         [3] => array(11) {          ["id"] => string(1) "4",  ["k"] => string(1) "V"        }}

Filter it like this

["Q"] => array(1) {        [0] => array(11) {          ["id"] => string(1) "1"          ["k"] => string(1) "Q"        }}["V"] => array(2) {        [0] => array(11) {          ["id"] => string(1) "3"          ["k"] => string(1) "V"        }         [1] => array(11) {          ["id"] => string(1) "4"          ["k"] => string(1) "V"        }}["H"] => array(1) {         [0] => array(11) {          ["id"] => string(1) "2"          ["k"] => string(1) "H"        }}

Reply content:

How to overwrite an array like this?

["Q"] => array(4) {        [0] => array(11) {          ["id"] => string(1) "1",["k"] => string(1) "Q"        }         [1] => array(11) {          ["id"] => string(1) "2",["k"] => string(1) "H"        }        [2] => array(11) {          ["id"] => string(1) "3", ["k"] => string(1) "V"        }         [3] => array(11) {          ["id"] => string(1) "4",  ["k"] => string(1) "V"        }}["V"] => array(4) {        [0] => array(11) {          ["id"] => string(1) "1",["k"] => string(1) "Q"        }         [1] => array(11) {          ["id"] => string(1) "2",["k"] => string(1) "H"        }        [2] => array(11) {          ["id"] => string(1) "3", ["k"] => string(1) "V"        }         [3] => array(11) {          ["id"] => string(1) "4",  ["k"] => string(1) "V"        }}["H"] => array(4) {        [0] => array(11) {          ["id"] => string(1) "1",["k"] => string(1) "Q"        }         [1] => array(11) {          ["id"] => string(1) "2",["k"] => string(1) "H"        }        [2] => array(11) {          ["id"] => string(1) "3", ["k"] => string(1) "V"        }         [3] => array(11) {          ["id"] => string(1) "4",  ["k"] => string(1) "V"        }}

Filter it like this

["Q"] => array(1) {        [0] => array(11) {          ["id"] => string(1) "1"          ["k"] => string(1) "Q"        }}["V"] => array(2) {        [0] => array(11) {          ["id"] => string(1) "3"          ["k"] => string(1) "V"        }         [1] => array(11) {          ["id"] => string(1) "4"          ["k"] => string(1) "V"        }}["H"] => array(1) {         [0] => array(11) {          ["id"] => string(1) "2"          ["k"] => string(1) "H"        }}


  array(        array(            'id'=>1,            'k'=>'Q'        ),        array(            'id'=>2,            'k'=>'H'        ),        array(            'id'=>3,            'k'=>'V'        ),        array(            'id'=>4,            'k'=>'V'        ),                        ),        'V'=>array(        array(            'id'=>1,            'k'=>'Q'        ),        array(            'id'=>2,            'k'=>'H'        ),        array(            'id'=>3,            'k'=>'V'        ),        array(            'id'=>4,            'k'=>'V'        ),        ),    'H'=>array(        array(            'id'=>1,            'k'=>'Q'        ),        array(            'id'=>2,            'k'=>'H'        ),        array(            'id'=>3,            'k'=>'V'        ),        array(            'id'=>4,            'k'=>'V'        ),        ),    );echo '
';print_r($arr);$new_arr = array();foreach($arr as $key=>$val) {    foreach($val as $k=>$v) {        if($key == $v['k']) {            $new_arr[$key][] = $v;        }    }}echo '';print_r($new_arr);

Array_filter ($ arr, fun );
In fun, the array key and k are used to determine whether the unset is not equal

$keys = array_keys($arr);$newArr = array();$i = 0;foreach ($arr as $key => $value) {    $newArr[$key] = array_filter($value, function ($val) {        GLOBAL $keys;        GLOBAL $i;        return $val["k"] == $keys[$i];    });     $i++;}

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.