0

I have an array such as below:

$import_emails = [];

$import_emails[] = [
    'to'      => $to,
    'from'    => $from,
    'cc'      => $cc,
    'subject' => $subject,
    'text'    => $text,
    'date'    => date('Y-m-d H:i:s', strtotime($date))
];

Example array data:

Array
     (
    [0] => Array
    (
        [to] => [email protected]
        [from] => babboe1 babboe1 <[email protected]>
        [cc] =>
        [subject] => Test Subject
        [text] => Test content.
        Please, write me later
        Thanks!

        [date] => 2017-06-29 18:04:53
    )

[1] => Array
    (
        [to] => Anastasia Gorobets <[email protected]>
        [from] => babboe1 babboe1 <[email protected]>
        [cc] => [email protected]
        [subject] => Tema
        [text] => Bla bla bla
         Test email! :)

        [date] => 2017-07-02 11:55:50
    )

 )

How can I check if a value, for example 'nastya' exists in array item ['to']? Is there maybe some function for it?

4
  • foreach + strpos - try something on your own. If you've problems, edit your question with what you've tried. Commented Jul 2, 2017 at 12:25
  • have a look at array_filter() and array_search(). That should give you some ideas Commented Mar 23, 2021 at 19:35
  • Or check this one: stackoverflow.com/a/31590256/3741900 Commented Mar 23, 2021 at 19:37
  • The values may not be unique. What then? Commented Aug 11 at 23:46

3 Answers 3

12

Sometimes I convert the associative array to a flat array using array_column. Then check if it exists.

$dataSubjectsValue = array_column($data, 'subject');
if (in_array('Test Subject', $dataSubjectsValue)) {
  // ...do your stuff
}

I think this is a little more readable than using foreach loops.

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

Comments

1

You need 'foreach' your array.

foreach ($import_emails as $key => $value) {

    $to = $value['to'];

    if($to == "nastya") {
        echo 'Found!';
        break;
    }
}

Comments

-1

You want to check if your value present in that array value.
( you want to find "nastya" in to field of array)

if( isset( $import_emails[$incex_of_array]['to']) )
    if(strpos($import_emails[]['to'], 'youvalue'))
        // you action 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.