1

For array with single values like

array(2) {
  ["blue"]=>
  int(0)
  ["red"]=>
  int(1)
}

I use this code

<?php
if (array_key_exists('blue',$array))
{
    echo "jo";
}
?>

But what code do I need to use for arrays like this

array(2) {
  ["yellow blue"]=>
  int(0)
  ["red white"]=>
  int(1)
}

to check if blue exists?

2
  • Do you want to check if the text blu exist in the key? Commented Nov 3, 2013 at 0:06
  • yes, check for 'yellow blue' works but not the check for blue only Commented Nov 3, 2013 at 0:07

5 Answers 5

2

My take:

if(preg_grep('/blue/', array_keys($array))) { echo 'found'; }

Or if you want to get them:

$matches = preg_grep('/blue/', array_keys($array));
print_r($matches);
Sign up to request clarification or add additional context in comments.

5 Comments

I saw other comments, you have to adjust the pattern if you don't want greenblue etc. You have to be specific about what you want.
your second code works but the first one looks better but the page isn't loading with it, is there may be an error in the code?
Yes, edited. But these are examples not copy paste for you. Does your editor not have some syntax highlighting?
No, which editor would you recommand? Your code works great :D
Depends. If you're serious about coding projects then check out an IDE (eclipse, netbeans, aptana, scores of others). Notepad++ is a good text editor for windows that has syntax highlighting. Not sure about Mac, but I use Linux and most if not all editors have some mode for programming of popular languages.
2
$partialKey = 'blue';
$byPartialKey = function ($key) use ($partialKey) {
    $parts = explode(' ', $key);
    return in_array($partialKey, $parts);
};

$result = array_filter (array_keys($input), $byPartialKey);

$result now contains all keys, that somehow contains $partialKey.

3 Comments

Thanks but how do I echo something if blue is found?
If $result if empty then echo "is there" or else "not there" right?
@MatthiasMüller With echo. Actually I don't know, whats the matter now. --- echo $result ? 'one or more "blue" found' : 'No "blue" found';
2

you can do using regular expression

search='blue';
foreach ($array as $key => $value) {
 if (preg_match('~'.$search.'~i',$key)) {
    echo "jo";
 }
}

The \b in the pattern indicates a word boundary, so only the distinct word "blue" is matched, and not a word partial like "bluegreen"

search='blue';
foreach ($array as $key => $value) {
 if (preg_match('/\b'.$search.'\b/i',$key)) {
    echo "jo";
 }
}

Reference

6 Comments

Thought of this too, but this also hits for example yellow bluegreen. Don't know, if this is an issue now, but changing requirements can make that an issue later, so I wouldn't rely on it.
You have to define the regular expression you want, only the word blue, o that contain blue in the string
I tried $search='blue'; foreach ($myarray as $key => $value) { if (preg_match('/\b'.$search.'\b/i',$key) { echo "jo"; } } but the page isn't loading
It was missing a close parenthesis in the IF, answer updated
Now it works too and is great for exact match. But for now i don't need exact match so I accepted AbraCadaver's solution
|
0

I would do it like this:

function subkey_exists($string, $array) {
    foreach($array as $key => $value) {
        $sub_keys = explode(" ", $key);
        if(in_array($string,$sub_keys)) {
            return true;
        }
    }
    return false;
}

[edit]

updated to your requirements

1 Comment

See my commet at Emilios answer: This also covers yell bluegreen
0

Yet another array_filter solution :

<?php
function find_keys_like($key, $array) {
    return array_filter(array_keys($array), function($k) use (&$key) {
        return strpos($k, $key) > -1;
    });
}
function array_has_key_like($key, $array) {
    return count(find_keys_like($key, $array)) > 0;
}

//test
$a = array('blue' => 1, 'yellow blue' => 2, 'green' => 3);
print_r(find_keys_like('blue', $a));
echo 'blue exists ? ' . (array_has_key_like('blue', $a) ? 'yes' : 'no') . PHP_EOL;
/** result :
Array
(
    [0] => blue
    [1] => yellow blue
)
blue exists ? yes
*/

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.