dsvf46980 2011-11-30 13:22
浏览 17
已采纳

PHP:Splice-like多索引数组删除/提取

Is there a native function to remove multiple indexes from an array and then return source array without removed objects or vice versa, to keep only required indexes while removing the rest.

Example:

$source = array('index1' => 1, 'index2' => 2, 'index3' => 3, 'index4' => 4);
json_encode( array_extract( $source, array('index2','index4'))); // returns array('index2' => 2, 'index4' => 4);

or:

json_encode( array_except( $source, array('index1','index4'))); // returns array('index2' => 2, 'index3' => 3);
  • 写回答

1条回答 默认 最新

  • dt250827 2011-11-30 13:27
    关注

    No, because there is a small problem, your first array contains keys and your second array contains values. There is no function to achieve what you want.

    There are multiple functions, array_intersect, array_diff, array_intersect_key, array_diff_key..but they all require matching arrays.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序