將PHP二維數組提取出自己想要的資訊

來源:互聯網
上載者:User
原二維數組,資訊很多

array (size=16)  'id' => string '64' (length=2)  'user_id' => string '64' (length=2)  'doc_id' => string '121' (length=3)  'time' => string '2015-08-07 11:41:28' (length=19)  'email' => string 'yintx_123@163.com' (length=17)  'username' => string '孫策' (length=6)  'password' => string '18bc4215375cd9773a5f572934018bfa' (length=32)  'phone' => string '456181687' (length=9)  'category' => string '2' (length=1)  'company' => string '東吳集團' (length=12)  'name' => string '孫策' (length=6)  'position' => string '我被社長乾死了' (length=21)  'address' => string '建康' (length=6)  'state' => string '3' (length=1)  'other_field' => null  'authentication' => string '1' (length=1)array (size=16)  'id' => string '65' (length=2)  'user_id' => string '65' (length=2)  'doc_id' => string '121' (length=3)  'time' => string '2015-08-11 04:27:08' (length=19)  'email' => string 'yintx_121@163.com' (length=17)  'username' => string '劉封' (length=6)  'password' => string '18bc4215375cd9773a5f572934018bfa' (length=32)  'phone' => string '' (length=0)  'category' => string '0' (length=1)  'company' => string '蜀漢' (length=6)  'name' => string '劉封' (length=6)  'position' => string '備胎' (length=6)  'address' => string '荊州阿薩斯' (length=15)  'state' => string '2' (length=1)  'other_field' => null  'authentication' => string '1' (length=1)

現在只需要email,company,name,phone這四個值,怎麼操作?

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)

回複內容:

原二維數組,資訊很多

array (size=16)  'id' => string '64' (length=2)  'user_id' => string '64' (length=2)  'doc_id' => string '121' (length=3)  'time' => string '2015-08-07 11:41:28' (length=19)  'email' => string 'yintx_123@163.com' (length=17)  'username' => string '孫策' (length=6)  'password' => string '18bc4215375cd9773a5f572934018bfa' (length=32)  'phone' => string '456181687' (length=9)  'category' => string '2' (length=1)  'company' => string '東吳集團' (length=12)  'name' => string '孫策' (length=6)  'position' => string '我被社長乾死了' (length=21)  'address' => string '建康' (length=6)  'state' => string '3' (length=1)  'other_field' => null  'authentication' => string '1' (length=1)array (size=16)  'id' => string '65' (length=2)  'user_id' => string '65' (length=2)  'doc_id' => string '121' (length=3)  'time' => string '2015-08-11 04:27:08' (length=19)  'email' => string 'yintx_121@163.com' (length=17)  'username' => string '劉封' (length=6)  'password' => string '18bc4215375cd9773a5f572934018bfa' (length=32)  'phone' => string '' (length=0)  'category' => string '0' (length=1)  'company' => string '蜀漢' (length=6)  'name' => string '劉封' (length=6)  'position' => string '備胎' (length=6)  'address' => string '荊州阿薩斯' (length=15)  'state' => string '2' (length=1)  'other_field' => null  'authentication' => string '1' (length=1)

現在只需要email,company,name,phone這四個值,怎麼操作?

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 下原數組,if (!in_array($key, $keys )),unset掉

$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        ))

來一個使用 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);
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.