duan051347 2013-04-25 16:59
浏览 23
已采纳

根据数组中给定的键提取数组部分

I have two arrays. One containing the data and other contains the keys. So I have

$data = array(
    'name' => array('label' => 'Name:', 'value' => 'Genghis'),
    'age' => array('label' => 'Age:', 'value' => '67'),
    'weigh' => array('label' => 'Weigh in Kgs:', 'value' => '78')
);

and

$keys = array('name', 'age');

Now I want to extract only the name and age elements of $data. Some thing like this.

$extracted = somemethod($data, $keys);
var_export($extracted);

Output should be like this.

array(
    'name' => array(
        'label' => 'Name:', 
        'value' => 'Genghis',
    ),
    'age' => array(
        'label' => 'Age:',
        'value' => '67',
    ),
)

How can i do this?

  • 写回答

3条回答 默认 最新

  • dqtiober37522 2013-04-25 17:11
    关注

    I would use an array_intersect_key() function like this:

    $data = array(...); // initial array as described
    $retained_keys = array('name' => 'value not used', 'age' => 'value not used');
    
    $filtered_array = array_intersect_key($data, $retained_keys);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试