doudun1934 2017-03-01 11:32
浏览 25
已采纳

使用PHP比较两个数组并在新数组中存储不常见的字符串

Below I have two arrays with values.

Array 1 - reorder_str

Array ( 
    [0] => A aacomputational agent is considered intelligent if it can adapt its actions to a particular setting. 
    [1] => A eecomputational agent is considered intelligent if it can adapt its actions to a particular setting. 
    [2] => defined 
    [3] => defined 
    [4] => defined 
) 

Array 2 -reorder1

Array ( 
    [0] => A bbcomputational agent is considered intelligent if it can adapt its actions to a particular setting. 
    [1] => A cccomputational agent is considered intelligent if it can adapt its actions to a particular setting. 
    [2] => A aacomputational agent is considered intelligent if it can adapt its actions to a particular setting. 
    [3] => A ddcomputational agent is considered intelligent if it can adapt its actions to a particular setting. 
    [4] => A eecomputational agent is considered intelligent if it can adapt its actions to a particular setting.
)

<?php if(in_array($reorder1[0], $reorder_str)){
 ?>
    <div class="qitem di">
        1.<?php echo $reorder1[0];?>
    </div>
    <?php }?>

My expected output array3

Array ( 
        [0] => A bbcomputational agent is considered intelligent if it can adapt its actions to a particular setting. 
        [1] => A cccomputational agent is considered intelligent if it can adapt its actions to a particular setting. 
         [2] => A ddcomputational agent is considered intelligent if it can adapt its actions to a particular setting. 
    )

I Want to show the string if it is not present in array(reorder_str).I tried like this but its not working out. My array 3 should store Uncommon strings from two array and new show not have any empty or undefined value.Thanks in advance.Meanwhile I tried with array_diff ,in_array but I couldn't get exact answer.

  • 写回答

1条回答 默认 最新

  • duanguilin2007 2017-03-01 11:48
    关注

    Check this code as per your expected output,

    $arr1 = array(
        0 => "A aacomputational agent is considered intelligent if it can adapt its actions to a particular setting.",
        1 => "A eecomputational agent is considered intelligent if it can adapt its actions to a particular setting.",
        2 => "defined ",
        3 => "defined ",
        4 => "defined ",
    );
    $arr1 = array_map('trim', $arr1);
    $arr2 = array(
        "0" => "A bbcomputational agent is considered intelligent if it can adapt its actions to a particular setting.",  
        "1" => "A cccomputational agent is considered intelligent if it can adapt its actions to a particular setting.",  
        "2" => "A aacomputational agent is considered intelligent if it can adapt its actions to a particular setting.",  
        "3" => "A ddcomputational agent is considered intelligent if it can adapt its actions to a particular setting.",  
        "4" => "A eecomputational agent is considered intelligent if it can adapt its actions to a particular setting."
    );
    $arr2 = array_map('trim', $arr2);
    $result = array_diff($arr2,$arr1);
    print_r($result);
    

    Here is working link

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

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错