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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)