0

I'm struggling with this..

I have a string like this.

$test2 = "AB:001,003;AC:001,003,004,005,008;AD:002,003,004,007,008,009";

It's two letters : 3 digits, (this can repeat) ;

This string will have multiple 2 letter prefixes, and the amount of 3 digit entries will vary.

I need to be able to find a match between a 2 letter prefix and an associated 3 digits number.

EG: Does AD003 exist in the string ? I know 003 appears mulitple times but I'd want to find just AD003, or another match I query for.

I thought converting the string to individual arrays named after the 2 letter prefixes would be the way to go..

$AB = array('001','003');
$AC = array('001','003','004',008);
$AD = array('002','003','004','007','008','009');

I've got as far as exploding it on the ; and then the :

$parts =  explode(";",$test2) ;
foreach ($parts as $value) {
    $a[] = explode(":", $value);
}
print_r ( $a );            

This results in :

Array
(
    [0] => Array
        (
            [0] => AB
            [1] => 001,002,003
        )

    [1] => Array
        (
            [0] => AC
            [1] => 001,003,004,005
        )

    [2] => Array
        (
            [0] => AD
            [1] => 002,003,004
        )

)

But I don't know how to take it further than that.

I want to be able to search the arrays for specific matches:

if (in_array("003", $AD)) { 
    echo "Match";
}

If there is an easier way than converting to array to find AD003 in the string, then I'm happy to try that.

Thanks

5 Answers 5

3

Or simply you can use preg_match to check in string

$string = "AB:001,003;AC:001,003,004,005,008;AD:002,003,004,007,008,009";

if( preg_match('/AD:([0-9,]+)?003/',$string)){
//Your code here
}
Sign up to request clarification or add additional context in comments.

4 Comments

this seems the best solution for the query you want to search for
Thanks this seems the simplest solution. is there any problem doing this if( ! preg_match('/AD:([0-9,]+)?003/',$string)){ ? Thanks
@Rocket Definitely no
? No there is no issue ? or no don't do it ? :) Thanks
1

Use the first array value as key:

$parts = explode(":", $value);
$key = array_shift($parts);
$a[$key] = $parts;

Then you can do:

if(isset($a['AD']) && in_array("003", $a['AD']))
  ...

Comments

1
$test2 = "AB:001,003;AC:001,003,004,005,008;AD:002,003,004,007,008,009";

$parts = explode( ';', $test2 );
foreach( $parts as $value ) {
    list( $key, $digits ) = explode( ':', $value );
    $a[$key] = explode( ',', $digits );
}

print_r( $a );

if( !empty( $a['AB'] ) && in_array( '003', $a['AB'] ) ) {
    echo "Yep, it's there.";
}

Here's the result:

Array
(
    [AB] => Array
        (
            [0] => 001
            [1] => 003
        )

    [AC] => Array
        (
            [0] => 001
            [1] => 003
            [2] => 004
            [3] => 005
            [4] => 008
        )

    [AD] => Array
        (
            [0] => 002
            [1] => 003
            [2] => 004
            [3] => 007
            [4] => 008
            [5] => 009
        )

)
Yep, it's there.

Comments

1

try this

<?php

$test2 = "AB:001,003;AC:001,003,004,005,008;AD:002,003,004,007,008,009";

$final = array();
$parts =  explode(";",$test2) ;
foreach ($parts as $value) {

    $a = explode(":", $value);
    foreach(explode(",", $a[1])as $val)
    {
       $final[$a[0].$val] =  $val;     
    }

}

echo "<pre>";
print_r ($final);

Result will be

Array
(
    [AB001] => 001
    [AB003] => 003
    [AC001] => 001
    [AC003] => 003
    [AC004] => 004
    [AC005] => 005
    [AC008] => 008
    [AD002] => 002
    [AD003] => 003
    [AD004] => 004
    [AD007] => 007
    [AD008] => 008
    [AD009] => 009
)

you can check if the value AB001 exists by using array_key_exists('AB001',$array);

1 Comment

the other 2nd best solutions other than regex, than can be used for searching your query
0

You can try this

<?php
// Example 1
$apple = "apple1 apple2 apple3 apple4 apple5 apple6";
$apples = explode(" ", $apple);
echo $apples[0]; // apple1
echo $apples[1]; // apple2...........

?>

Comments

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.