4

I have the following list (example codes, variable $code_name): "A125" "B120" "C105"

And an array ($codes_list) with a lot of codes but also with some extra words in them: "A125 NameA" "B8800 Ko" "B120 Name Bc" "D3030"

Within a for loop I can check if any of the values ($code_name) from the above list exists in the array.

if (in_array($code_name, $codes_list))
  {
    echo "Do nothing<br/>";
  } else {
    echo "Code is not in the list, create new one in DB: ".$code_name."<br/>";  
  }

The problem as I said is that the $code_name contains only the "A125". But in the array list some additional text is added there "A125 NameA". So the result won't be what I want it to be.

If code name ALREADY EXISTS in the list (like A125,B120 for example) then do nothing. If it doesn't exist (C105) then create one in DB.

But what I'm trying to achieve is to check whether the $code_name LIKE%% in $codes_list array. So I'm trying to find a similar function to the mysql one.

Is that possible?

if (in_array(LIKE%'.$code_name.'%, $codes_list))

Thanks for the help

8
  • 2
    This seems to have nothing to do with mysql, so I'm removing the tag. Commented May 27, 2015 at 11:17
  • 2
    Its really unclear. What you want to achieve. Commented May 27, 2015 at 11:18
  • You can use strpos() php.net/strpos Commented May 27, 2015 at 11:18
  • a substring search through the elements in an array? Commented May 27, 2015 at 11:19
  • What i want to achieve Uchiha is the following: If code name doesn't exist in the array list, then I will create a new DB row with the appropriate data. As you can see in my example I have two code names that already exist so I don't have to do anything with them. But first I need to check if they exist in the array list. In the arraylist they are structured a bit different as they contain their code name but also some additional text, thus, the in_array function won't work. Thanks :) Commented May 27, 2015 at 11:40

4 Answers 4

3

You can use preg_grep for this. You can search with exact pattern.

  $check = preg_grep("/A125/", $codes_list);
  if (!empty($check))
  {
    echo "Do nothing<br/>";
  } else {
    echo "Code is not in the list, create new one in DB: ".$code_name."<br/>";  
  }

preg_grep will return array with matching elements if any match found else will return an empty array. You just have check if the returned array is empty or not.

Example

$array = array("abc123 kj", "b45 kl", "f34");

var_dump(preg_grep("/abc123/", $array));

Output

array(1) {
  [0]=>
  string(9) "abc123 kj"
}

preg_grep

Sign up to request clarification or add additional context in comments.

3 Comments

First code that you tell is going to give Fatal error: Can't use function return value in write context .thanks. Check Please.
It will depend on the php version. though have updated the code. :)
@anantkumarsingh it was not wrong. Older php version will generate that error.
1
foreach($codes_list as $key => $value) {
if (strpos($value, $code_name) !== false) {
       //Do Nothing
    }
    else{

      //The code doesn't match

   }
}

1 Comment

The trouble with this answer is that the false or true condition will appear for each value in codes_list... an not only one for the whole array.
1

Another solution could be something like :

if (strpos(implode( '§', $codes_list ),$code_name)===false) {
 echo "Code is not in the list, create new one in DB: ".$code_name."<br/>";  
} else {
 echo "Do nothing<br/>";
}

Comments

0

I think you are searching for this:-

    <?php
    $code_array = array("A125 NameA");
    $abc = 'A125';

    print_r( preg_grep( "/".$abc."/" , $code_array ) );

     $check = preg_grep( "/".$abc."/" , $code_array );
  if (!empty($check))
  {
    echo "Do nothing<br/>";
  } else {
    echo "Code is not in the list, create new one in DB: ".$code_name."<br/>";  
  }

    ?>

Output:-http://prntscr.com/79xylb

Note:- preg_grep will produce an array with matched elements (if any match found) otherwise empty array.

2 Comments

Hello, thank you for the suggestion. The problem is that I have a lot of variables ($code_name). Maybe more than 100. And with different structure. So in the array it could be "A125 NameA" or "NameA A125" or "A1 NameA A125". What I'm looking for is to match the $code_name="A125" to one of the elements inside the array. So exactly how LIKE%% mysql function works :)
it will work . no matter how many element are there and what are there pattern. Just try

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.