Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7788121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:53:37+00:00 2026-06-01T20:53:37+00:00

I thought this would be a simple thing to do with a native php

  • 0

I thought this would be a simple thing to do with a native php function but i’ve found a few different, all quite complicated, ways that people have tried to achieve it. What’s the most efficient way of checking if a string contains one or more elements in an array? i.e, below – where $data[‘description’] is a string. Obv the in_array check below breaks because it expects param 2 to be an array

$keywords = array(
            'bus',
            'buses',
            'train',
    );

    if (!in_array($keywords, $data['description']))
            continue;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:53:39+00:00Added an answer on June 1, 2026 at 8:53 pm

    Assuming that the String is a collapsed/delimited list of values

    function arrayInString( $inArray , $inString , $inDelim=',' ){
      $inStringAsArray = explode( $inDelim , $inString );
      return ( count( array_intersect( $inArray , $inStringAsArray ) )>0 );
    }
    

    Example 1:

    arrayInString( array( 'red' , 'blue' ) , 'red,white,orange' , ',' );
    // Would return true
    // When 'red,white,orange' are split by ',',
    // the 'red' element matched the array
    

    Example 2:

    arrayInString( array( 'mouse' , 'cat' ) , 'mouse' );
    // Would return true
    // When 'mouse' is split by ',' (the default deliminator),
    // the 'mouse' element matches the array which contains only 'mouse'
    

    Assuming that the String is plain text, and you are simply looking for instances of the specified words inside it

    function arrayInString( $inArray , $inString ){
      if( is_array( $inArray ) ){
        foreach( $inArray as $e ){
          if( strpos( $inString , $e )!==false )
            return true;
        }
        return false;
      }else{
        return ( strpos( $inString , $inArray )!==false );
      }
    }
    

    Example 1:

    arrayInString( array( 'apple' , 'banana' ) , 'I ate an apple' );
    // Would return true
    // As 'I ate an apple' contains 'apple'
    

    Example 2:

    arrayInString( array( 'car' , 'bus' ) , 'I was busy' );
    // Would return true
    // As 'bus' is present in the string, even though it is part of 'busy'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought this would be a really simple thing, but I'm having a hard
Hey guys, I thought this would be an incredibly simple thing to do, but
I thought this would be simple, but its not working .. in both the
OK so I thought this would be pretty simple but it's tripping me up..
I thought it would be as simple as this... $('#yourSelect').change(function () { if ($(this).val()
I thought this would be pretty easy but I'm running into all sorts of
This would be simple to track down in Objective-C, but in Java I thought
I'm trying to do something that I thought would be quite simple but as
This is what I thought would be a simple select clause, however the following
Thanks for reading this. I would have thought it would be as simple as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.