One [key value] in a two-dimensional array is used as the one-dimensional [Key]

Source: Internet
Author: User
If you do not use foreach to traverse an array of {code...} such as code...}, how can we achieve the following results? {code...} tried array_map and failed.
$ Array = array (0 => array ('univs _ id' => 1001, 'univs _ name' => 'tsinghua Emy ', 'province _ id' => 110000, 'province '=> 'beijing',), 1 => array ('univs _ id' => 1002, 'univs _ name' => 'peking University ', 'province _ id' => 110000, 'province '=> 'beijing',), 2 => array ('univs _ id' => 1003, 'univs _ name' => 'renmin University of China ', 'province _ id' => 110000, 'province' => 'beijing ',),);

If you do not use foreach to traverse such an array, how can we achieve the following results?

$ Array = array (1001 => array ('univs _ id' => 1001, 'univs _ name' => 'tsinghua Emy ', 'province _ id' => 110000, 'province '=> 'beijing',), 1002 => array ('univs _ id' => 1002, 'univs _ name' => 'peking University ', 'province _ id' => 110000, 'province '=> 'beijing',), 1003 => array ('univs _ id' => 1003, 'univs _ name' => 'renmin University of China ', 'province _ id' => 110000, 'province' => 'beijing ',),);

Failed to try array_map.

Reply content:
$ Array = array (0 => array ('univs _ id' => 1001, 'univs _ name' => 'tsinghua Emy ', 'province _ id' => 110000, 'province '=> 'beijing',), 1 => array ('univs _ id' => 1002, 'univs _ name' => 'peking University ', 'province _ id' => 110000, 'province '=> 'beijing',), 2 => array ('univs _ id' => 1003, 'univs _ name' => 'renmin University of China ', 'province _ id' => 110000, 'province' => 'beijing ',),);

If you do not use foreach to traverse such an array, how can we achieve the following results?

$ Array = array (1001 => array ('univs _ id' => 1001, 'univs _ name' => 'tsinghua Emy ', 'province _ id' => 110000, 'province '=> 'beijing',), 1002 => array ('univs _ id' => 1002, 'univs _ name' => 'peking University ', 'province _ id' => 110000, 'province '=> 'beijing',), 1003 => array ('univs _ id' => 1003, 'univs _ name' => 'renmin University of China ', 'province _ id' => 110000, 'province' => 'beijing ',),);

Failed to try array_map.

$ Array = [Your array] $ newArray = array_reduce ($ array, function (& $ newArray, $ v) {$ newArray [$ v ['univs _ id'] = $ v; return $ newArray ;});

$ NewArray is the array you want

$ New_array = array_combine (array_column ($ array, 'univs _ id'), $ array );

Array_column can be used at php5.5 or above.
Earlier array_column implementations

if(!function_exists('array_column')){     function array_column($array,$key){        return array_map(            function($element,$key){return $element[$key];},            $array,            array_fill(0,count($array),$key)        );    }}

The key name of the array cannot be changed directly in php. You need to add a new item and delete the old item.

$arr[$newkey] = $arr[$oldkey];unset($arr[$oldkey]);

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.