douweidao3882 2013-02-28 19:11
浏览 181
已采纳

如何检查字符串数组中是否存在字符串

I have data stored in a string: such as $string='12';
'12' represents 2 pieces of data, 1 and 2

In 3 columns, I have those same ids stored, the array comes out as :

ex1: $array= ('num1' => 1, 'num2' => 2, 'num3' => )
ex2: $array= ('num1' => 5, 'num2' => 4, 'num3' => 3)

How could I compare these and return true if the numbers exist in both places?

For instance: using the above example: for ex1: 12 would return TRUE

34 would return FALSE for ex2: 345 would return TRUE

  • 写回答

4条回答 默认 最新

  • doujiao3346 2013-02-28 20:28
    关注

    This works, please see results below: (Value must be present in both array to return true.

      $result = true;
        $string1 = '43';
        $string = str_split($string1);
        $example1 = array('num1' => 1, 'num2' => 2, 'num3' => 3,  'num4' => 4, 'num5' => 5);
        $example2 = array('num1' => 5, 'num2' => 4, 'num3' => 3);
        foreach ($string as $st) {
            if((in_array($st, $example1) && in_array($st, $example2))  && $result == true){
                $result = true; //true
            } else {
                $result = false;
            }
        }
        if($result == true){
                echo 1; //true
            } else {
                echo 0; //false
            }
        exit;
        //Test Results:
        //$string1 = '12'; //result 0
        //$string1 = '34'; //result 1
        //$string1 = '55'; //result 1
        //$string1 = '43'; //result 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决