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 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛