doolo00026 2016-01-07 05:15
浏览 109
已采纳

使用此数组在PHP中映射数组

I have array as follows,

$arr['fixed_key'][0]['key1']['key2'] = 'y';
$arr['fixed_key'][1]['key1']['key2'] = 'y';
$arr['fixed_key'][2]['key1']['key2'] = 'n';
$arr['fixed_key'][3]['key1']['key2'] = 'n';

I want to remove all arrays which have key2='n', I can traverse through array in a loop and achieve this, as follows:

$l=length($arr);
for($i=0;$i<$l;$i++) {

    if($arr['fixed_key'][$i]['key1']['key2'] == 'n') {

        unset($arr['fixed_key'][$i]['key1']['key2']);

    }

}

My question is, is it possible to do this in better way, like array_map or array_walk, what is the best possible way?

  • 写回答

2条回答 默认 最新

  • duanqiao2225 2016-01-07 05:34
    关注

    You can use array_filter

    $arr['fixed_key'][0]['key1']['key2'] = 'y';
    $arr['fixed_key'][1]['key1']['key2'] = 'y';
    $arr['fixed_key'][2]['key1']['key2'] = 'n';
    $arr['fixed_key'][3]['key1']['key2'] = 'n';
    
    $filtered = array_filter($arr['fixed_key'], function ($val) { return $val['key1']['key2']  !== "n"; });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络