php check if value exists in associative array

php check if value exists in multidimensional array
php check if value exists in array
php check if key exists in multidimensional array
php check if key exists in array
php search multidimensional array
php in_array multidimensional
array push php
array search php

I have array such 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] => nastya.gorobets95@gmail.com
        [from] => babboe1 babboe1 <test.babboe1@gmail.com>
        [cc] => 
        [subject] => Test Subject
        [text] => Test content.
        Please, write me later
        Thanks!

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

[1] => Array
    (
        [to] => Anastasia Gorobets <nastya.gorobets95@gmail.com>
        [from] => babboe1 babboe1 <test.babboe1@gmail.com>
        [cc] => babboesignal@edu-crm.com
        [subject] => Tema
        [text] => Bla bla bla
         Test email! :)

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

 )

How can I check if value, for example 'nastya' exists in arrays item ['to'] ? Maybe there is some function for it? Thanks!

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.You could also do something similar with array_map.

How to check if an array value exists?, PHP 5, PHP 7). array_key_exists — Checks if the given key or index exists in the array Parameters �. key. Value to check. array. An array with keys to check. Parameters. needle. The searched value. Note: . If needle is a string, the comparison is done in a case-sensitive manner.. haystack. The array. strict. If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.

You need foreach your array.

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

$to = $value['to'];

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

}

array_key_exists - Manual, in_array — Checks if a value exists in an array It impossible to receive false as a return value from in_array using loose checking if your arrays contains both the an associative array within a haystack of associative arrays in strict mode if� PHP array_search() method to check if a value exists in an array in PHP. PHP array_search() method search an array for given value and return the corresponding key if a value exists in an array. If a value doesn’t exist in an array then it returns NULL. How to sort string using PHP code. Syntax –

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 

in_array - Manual, In PHP 5.5 and later you can use array_column() in conjunction with array_search() . This is particularly useful for checking if a value exists in an associative� PHP: Checks if the given key or index exists in an array. The array_key_exists() function is used to check whether a specified key is present in an array or not. The function returns TRUE if the given key is set in the array. The key can be any value possible for an array index. Version: (PHP 4 and above) Syntax: array_key_exists(array_key

PHP, PHP Array Reference. Example. Check if the key "Volvo" exists in an array: array, an integer key is generated, starting at 0 and increases by 1 for each value. The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist. Tip: Remember that if you skip the key when you specify an array, an integer key is generated, starting at 0 and increases by 1 for each value. (See example below)

PHP array_key_exists() Function, How to Check if a value exists in an Associative array in PHP. In above example, we have used in_array() method for a simple array. Let's check� Return Values. Returns TRUE on success or FALSE on failure.. Note: . array_key_exists() will search for the keys in the first dimension only. Nested keys in multidimensional arrays will not be found.

How to Check if a Value Exists in an Array in PHP, The in_array() function is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given� The "in_array" function of php checks if a value exists in an array.But it don't work properly with associative array. Here is a function which will work properly with associative array.

Comments
  • foreach + strpos - try something on your own. If you've problems, edit your question with what you've tried.