1

I have a multidimensional array. They keys of the separate arrays are text. I would like to know how to match the key with part of a string that's from looped SELECT data.

Example of Array

Array ( [Volvo] => Array ( [0] => ) 
        [Ford] => Array ( [0] => ) 
        [Seat] => Array ( [0] => ) )

So from this array $cars, how would I be able to match data coming from my SELECT query

Select Output echo $row['car'];

Volvo, Mercedes, Lexus

What I'm after

if the key of one of my arrays matches part of the string from my select query, so as the example says above Volvo would match, then do something. That something for me is adding more data in that array.

I have tried variations of in_array, array_search and array_filter but have not found a solution that works for me.

Example of the reverse of what i was doing through a foreach loop

    $searchword = $array_element;
$result = array_filter($cars, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });

taken from: Search for PHP array element containing string

3
  • 1
    I have tried variations of Just show us 1 attempt and we will show you where you got stuck and how to solve your problem. Commented Aug 11, 2015 at 16:06
  • 1
    @Rizier123 i posted an edit. I didnt like this one as it doesnt work with MD arrays and i dont want to loop every array key for speed reasons. All other variations are the same as the one above, useless. Commented Aug 11, 2015 at 16:12
  • useless, attempts are never useless! It helps you to learn stuff. Trying stuff out and see what it does helps a lot to learn stuff. Commented Aug 11, 2015 at 16:14

3 Answers 3

1

This should work for you:

First I used preg_split() to split your search string (Volvo, Mercedes, Lexus) into an array.

The regex:

/\s*,\s*/

Simply means this:

  • \s* match any white space character [\r\n\t\f ]
    • Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
  • , matches the character , literally
  • \s* match any white space character [\r\n\t\f ]
    • Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]

This makes sure we don't have spaces in the search words from your string. So with this we will end up with an array like this:

Array
(
    [0] => Volvo
    [1] => Mercedes
    [2] => Lexus
)

After that I just take the array_intersect_key() from your array and the search array, which we just created.

Code:

<?php

    $arr = preg_split("/\s*,\s*/", $row["car"], -1, PREG_SPLIT_NO_EMPTY);

    if(($intersect = array_intersect_key($cars, array_flip($arr)))) {
        echo "Do stuff!";
        print_r($intersect);
    } else {
        echo "Drink a cup of tea";
    }

?>

output:

Array
(
    [Volvo] => Array
        (
            [0] => 
        )

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

1 Comment

You star! i only had to make a couple of adjustments to 100% get what i need so thank you very much @Rizier123
1

There are many ways to achieve your goal. I would use function explode() to split the data retrieved from the database ($row['car']) into pieces and trim() to be sure the identified pieces do not have padding spaces:

$brands = array_map('trim', explode(',', $row['car']));

Now, print_r($brands) should reveal an array whose values are candidate keys in your example array (called $example below).

Next it depends on what you want to match and how you want to process the matches.

A simple identification can be done using array_intersect() with $brands and the keys of the example array (see function array_keys()).

$common = array_intersect($brands, array_keys($example));
foreach ($common as $key) {
    // Do something with $example[$key]
}

Or you can iterate directly over $brands and check if the value is a key in $example:

foreach ($brands as $key) {
    if (array_key_exists($key, $example)) {
        // Do something with $example[$key]
    }
}

You can use isset($example[$key]) as well, instead of array_key_exists($key, $example]).

Comments

1

Try using PHP stripos, it's simple and effective:

Base data:

$array = Array ( [Volvo] => Array ( [0] => ) 
        [Ford] => Array ( [0] => ) 
        [Seat] => Array ( [0] => ) )
;

SQL data (string or array):

$row['car']; // string
$row //array of all SQL columns output

Foreach with stripos:

    foreach($array as $rowKey=>$rowArray){
        $rowKey = trim($rowKey);
        foreach($row as $sqlRow=>$sqlArray){
            $sqlRow = trim($sqlRow);
            if (stripos($rowKey,$sqlRow) !== false){
               ///values are the same in this key in the array and in the SQL output
            }
         }
     unset($sqlRow,$sqlArray);
    }
unset($rowKey,$rowArray);

This may not be the most efficient but should give you the result you want

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.