Home > Backend Development > PHP Tutorial > 将PHP二维数组提取出自己想要的信息

将PHP二维数组提取出自己想要的信息

WBOY
Release: 2016-06-06 20:29:08
Original
1257 people have browsed it

原二维数组,信息很多

<code>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)
</code>
Copy after login
Copy after login

现在只需要email,company,name,phone这四个值,怎么操作?

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

回复内容:

原二维数组,信息很多

<code>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)
</code>
Copy after login
Copy after login

现在只需要email,company,name,phone这四个值,怎么操作?

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

$keys = array(email,company,name,phone);
foreach 下原数组,if (!in_array($key, $keys )),unset掉

<code class="php">$result = array_map(function($value){
  return ['email'=>$value['email'],'company'=>$value['company'],'name'=>$value['name'],'phone'=>$value['phone']];
}, $arr);
var_dump($result);</code>
Copy after login

<code><?php header('Content-Type: text/plain; charset=utf-8');
$arr = array(
    0 => 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
        )

)</code>
Copy after login

来一个使用 array_walk() 的方法

<code>$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);</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template