Extract the information you want from a two-dimensional PHP array

Source: Internet
Author: User
The original two-dimensional array, a lot of information

Array (size=16) ' id ' = = String ' "(length=2) ' user_id ' = ' + String '" (length=2) ' doc_id ' = = String ' 121 ' (l ength=3) ' time ' = = String ' 2015-08-07 11:41:28 ' (length=19) ' email ' = = String ' yintx_123@163.com ' (length=17) ' Us Ername ' = = String ' Sun-policy ' (length=6) ' password ' + string ' 18BC4215375CD9773A5F572934018BFA ' (length=32) ' phone ' =&gt ; String ' 456181687 ' (length=9) ' category ' = = String ' 2 ' (length=1) ' company ' = = String ' Dong Wu Group ' (length=12) ' Name ' = > String ' Sun ' (length=6) ' position ' + string ' I was killed by the President ' (length=21) ' address ' and string ' Jian Kang ' (length=6) ' state  ' = = String ' 3 ' (length=1) ' Other_field ' + null ' authentication ' = = String ' 1 ' (length=1) array (size=16) ' ID ' = = String ' (length=2) ' user_id ' = ' + String ' (length=2) ' doc_id ' = = String ' 121 ' (length=3) ' Time ' =&gt ; String ' 2015-08-11 04:27:08 ' (length=19) ' email ' + string ' yintx_121@163.com ' (length=17) ' username ' + string ' Liu Seal ' (length=6) ' PassWord ' = ' + string ' 18BC4215375CD9773A5F572934018BFA ' (length=32) ' phone ' = ' + String ' (length=0) ' category ' = = St Ring ' 0 ' (length=1) ' company ' = = String ' Shuhan ' (length=6) ' name ' = + string ' Liu Feng ' (length=6) ' position ' = = string   ' Spare tire ' (length=6) ' address ' = = String ' Jingzhou arthas ' (length=15) ' state ' = = String ' 2 ' (length=1) ' other_field ' = null ' Authentication ' = String ' 1 ' (length=1)

Now only need to email,company,name,phone these four values, how to operate?

array (size=16)  'email' => string 'yintx_123@163.com' (length=17)  'phone' => string '456181687' (length=9)  'company' => string '东吴集团' (length=12)  'name' => string '孙策' (length=6)
array (size=16)  'email' => string 'yintx_121@163.com' (length=17)  'phone' => string '' (length=0)  'company' => string '蜀汉' (length=6)  'name' => string '刘封' (length=6)

Reply content:

The original two-dimensional array, a lot of information

Array (size=16) ' id ' = = String ' "(length=2) ' user_id ' = ' + String '" (length=2) ' doc_id ' = = String ' 121 ' (l ength=3) ' time ' = = String ' 2015-08-07 11:41:28 ' (length=19) ' email ' = = String ' yintx_123@163.com ' (length=17) ' Us Ername ' = = String ' Sun-policy ' (length=6) ' password ' + string ' 18BC4215375CD9773A5F572934018BFA ' (length=32) ' phone ' =&gt ; String ' 456181687 ' (length=9) ' category ' = = String ' 2 ' (length=1) ' company ' = = String ' Dong Wu Group ' (length=12) ' Name ' = > String ' Sun ' (length=6) ' position ' + string ' I was killed by the President ' (length=21) ' address ' and string ' Jian Kang ' (length=6) ' state  ' = = String ' 3 ' (length=1) ' Other_field ' + null ' authentication ' = = String ' 1 ' (length=1) array (size=16) ' ID ' = = String ' (length=2) ' user_id ' = ' + String ' (length=2) ' doc_id ' = = String ' 121 ' (length=3) ' Time ' =&gt ; String ' 2015-08-11 04:27:08 ' (length=19) ' email ' + string ' yintx_121@163.com ' (length=17) ' username ' + string ' Liu Seal ' (length=6) ' PassWord ' = ' + string ' 18BC4215375CD9773A5F572934018BFA ' (length=32) ' phone ' = ' + String ' (length=0) ' category ' = = St Ring ' 0 ' (length=1) ' company ' = = String ' Shuhan ' (length=6) ' name ' = + string ' Liu Feng ' (length=6) ' position ' = = string   ' Spare tire ' (length=6) ' address ' = = String ' Jingzhou arthas ' (length=15) ' state ' = = String ' 2 ' (length=1) ' other_field ' = null ' Authentication ' = String ' 1 ' (length=1)

Now only need to email,company,name,phone these four values, how to operate?

array (size=16)  'email' => string 'yintx_123@163.com' (length=17)  'phone' => string '456181687' (length=9)  'company' => string '东吴集团' (length=12)  'name' => string '孙策' (length=6)
array (size=16)  'email' => string 'yintx_121@163.com' (length=17)  'phone' => string '' (length=0)  'company' => string '蜀汉' (length=6)  'name' => string '刘封' (length=6)

$keys = Array (email,company,name,phone);
foreach under the original array, if (! In_array ($key, $keys)), unset off

$result = array_map(function($value){  return ['email'=>$value['email'],'company'=>$value['company'],'name'=>$value['name'],'phone'=>$value['phone']];}, $arr);var_dump($result);


  
    array(        'id' => 1,        'name' => 'Apache',        'version' => '2.4'    ),    1 => array(        'id' => 2,        'name' => 'Nginx',        'version' => '1.8'    ));$new = array();foreach($arr as $k => $v) {    $new[$k]['name'] = $v['name'];    $new[$k]['version'] = $v['version'];}print_r($new);//输出:Array(    [0] => Array        (            [name] => Apache            [version] => 2.4        )    [1] => Array        (            [name] => Nginx            [version] => 1.8        ))

A way to use it array_walk() .

$array = array(    array(        'id' => 1,        'name' => 'xiaoming',        'age' => 14,        'addr' => 'beijing',    ),    array(        'id' => 2,        'name' => 'xiaohong',        'age' => 18,        'addr' => 'tianjin',    ),);function unEvent(&$value) {    unset($value['age']);    unset($value['addr']);}array_walk($array, 'unEvent');var_dump($array);
  • 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.