Search a multidimensional array for more than one specific value

php search multidimensional array for multiple values
php search multidimensional array for value
php check if value exists in multidimensional array return key
php array search multiple values
get value from multidimensional array php
php get array key by value
array_search
php search array of objects

I have a multidimensional array that looks something like this:

Array
(
    [0] => Array
            (
                [day] => Every Sunday
                [time] => 08:00
                [form] => normal
            )

    [1] => Array
            (
                [day] => Every Monday
                [time] => 10:30
                [form] => special 
            )

    [2] => Array
            (
                [day] => Every Wednesday
                [time] => 17:00
                [form] => normal
            )
)

I'd like to check if it contains specific days. For example, I'd like to check if it contains Monday and Sunday.

Using array_search works fine for one day. E.g. if I only want to check for Sunday:

array_search('Sunday', array_column($times, 'day'));

But it returns nothing if I pass in more than one day and attempt to search for Sunday and Monday:

array_search(array('Sunday', 'Monday'), array_column($times, 'day'));

Is there another way of doing this, preferably without iterating through everything using a for loop?

Your array for times defines the key 'day' twice, so this array will not be valid.

try changing your input array to

$times = array(
    'Monday',
    'Sunday
);

Secondly, array_search() cannot accept an array as its first value, as it is expecting a search term please see:

http://php.net/manual/en/function.array-search.php

As array search returns the Key of the array of the found value, this is not what you need here, you could use:

http://php.net/manual/en/function.array-count-values.php

this will grab a unique list of values in your input array and return a key => value set with the value => count as a new array.

$times = array('Monday','Tuesday','Wednesday','Monday','Monday');
print_r(array_count_values($times));

should return:

array (
    [Monday] => 3
    [Tuesday] => 1
    [Wednesday] => 1
)

UPDATE:

After op gave a multidimensional array, the solution would be

array_count_values(array_column($array, $key));

How to search an multi-dimensional array with multiple search , I want to add pseudo in the search condition too (the keys journee and pseudo should be searched for a specific values). How would I accomplish this? share. PHP multidimensional array search by value. In PHP, multidimensional array search refers to searching a value in a multilevel nested array. There are various techniques to carry out this type of search, such as iterating over nested arrays, recursive approaches and inbuilt array search functions. Iterative Approach: Iterating over the array and searching for significant match is the simplest approach one can follow.

You can use;

print_r(array_count_values($times));

PHP multidimensional array search by value, Update: I've been making some simple benchmarks and the multiple results form array multidimensional * @param string value to search for, ie a specific field� In a multidimensional array, if there is no unique pair of key => value (more than one pair of key => value) exists then in that case if we search the element by a single key => value pair then it can return more than one items. Therefore we can implement the search with more than one key => value pair to get unique items.

One issue is that the needle for array_search is explicit, unlike strpos. So Sunday will not match a value of Every Sunday, assuming that One Sunday or Every Sunday may be desired to be searched for.

Since you already know the key to search for, I suggest using array_filterwith strpos instead. Then you can simply use count($filteredTimes) to determine how many or use array_keys($filteredTimes) to retrieve the keys as it would array_search;

It will be the equivalent of using array_column and array_search or array_count_values or array_keys($array, $search). But instead of only returning an array of keys (or single key) with 2 iterations, it will return the original array values that contain the desired criteria or be empty, with 1 iteration.

Example: https://3v4l.org/uCUMr
$filteredTimes = array_filter($times, function($v) {
    return isset($v['day']) && (false !== strpos($v['day'], 'Sunday') || false !== strpos($v['day'], 'Monday'));
});
$matchingDays = count($filteredTimes);

var_export($filteredTimes);
var_dump($matchingDays);

Result

array (
  0 => 
  array (
    'day' => 'Every Sunday',
    'time' => '08:00',
    'form' => 'normal',
  ),
  1 => 
  array (
    'day' => 'Every Monday',
    'time' => '10:30',
    'form' => 'special',
  ),
)

int(2)

array_search - Manual, array_search — Searches the array for a given value and returns the first If needle is found in haystack more than once, the first matching key is returned. don't have to write your own function to search through a multi dimensional array If an array has more than one dimension it is called multidimensional array. If you have a list of names which you want to put to an array, you can put them in an array which has single dimension. But think you need to store these names with address and age of each person separately, then the best way is to use an array with two dimensions.

Array Functions - Manual, elements of the given arrays; array_merge_recursive — Merge one or more arrays I need to take an element from the Array and change its position within the Here is a function to find out the maximum depth of a multidimensional array. In Java programming, We can use the index position to access the Java multi dimensional array elements. Using the index, we can access or alter/change every individual element present in the multi dimensional array. The index value of Multi Dimensional Array in Java starts at 0. It ends at n-1, where n is the size of tables, row, or column.

PHP multidimensional array search by value, Iterating over the array and searching for significant match is the simplest approach one can follow. Check if an element of the given array is itself an array or not� An Excel array formula is a formula that carries out calculations on the values in one or more arrays rather than a single data value. In spreadsheet programs, an array is a range or series of related data values that are usually in adjacent cells in a worksheet.

How to search by multiple key => value in PHP array, In a multidimensional array, if there is no unique pair of key => value (more than one pair of key => value) exists then in that case if we search the element by a� For more information, see Array Indexing. Some problems require information about the locations of the array elements that meet a condition rather than their actual values. In this example, you can use the find function to locate all of the elements in A less than 9.

Comments
  • I'd like to check if it contains several days. Are you interested in knowing if array contains more than one Occurance OR that is contains some specific days
  • Hi Riggs, I'd like to check if it contains specific occurances. E.g. Maybe Tuesday and Thursday. Maybe Sunday and Monday.
  • You do realise that example array is not valid I suppose?
  • @RiggsFolly I've updated now.
  • Stupid question, (but I always like to check these types of things) do you really mean 'Sunday, Monday'? 'Sunday', 'Monday' would actually make an array of "Sunday" and Monday. The example you posted makes an array with 1 element: "Sunday, Monday". I'm sure it's just a typo, but you never know.
  • Thank you. Unfortunately I cannot change the original array from a multidimensional array.
  • HI, the array you provided is not multidimensional, as it only contains one set of key => values, an array is only multidimensional if a value itself contains an array. Even an array like the one in my post will be assigned keys as 0, 1, 2 etc as that is the default behaviour, array_count_values() will ignore the keys regardless of what they are, a long as they are unique.
  • @MHewson sorry, I've updated now with some real data.
  • Ah now it makes more sense. You could use the same method but instead of array_count_values($times); you could do array_count_values(array_column($times, 'day'));
  • Great thanks, that is returning a simple array now. So if I just want to know if there's a 'Monday' and 'Wednesday' in there, how could I do that?