doudian7996 2017-05-10 02:05
浏览 79
已采纳

从基于另一个数组的数组中删除值

I'm gonna need a little help from you guys to fix this little code. The idea is to remove any numbers that are inside $remove_str from $list_str. As you can see I've already tried to solve the problem by turning both strings into arrays and simply loop through the list array searching for values inside the remove array and remove it if there's a match. However, the results are anything but what I expected. I've been toying around with it for a while now, but my head is spinning to much to see the solution.

<?php

$remove_str = '5,6,8,56,195';
$list_str = '1,3,6,9,34,150,195,213';

$remove_arr = explode(',', $remove_str);
$list_arr = explode(',', $list_str);

foreach($list_arr as $value){
    $position = array_search($value, $remove_arr);

    if($position !== false){
        unset($list_arr[$position]);
    } else {
        continue;
    }
}

$result = implode(',', $list_arr);

echo $result;

?>

Result:

1,6,9,150,195,213

Expected result:

1,3,9,34,150,213

  • 写回答

1条回答 默认 最新

  • douxianliu6756 2017-05-10 02:06
    关注

    You can use array_diff,

    array_diff($list_arr, $remove_arr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?