Skip to content

Instantly share code, notes, and snippets.

@rseon
Last active May 6, 2021 12:30
Show Gist options
  • Save rseon/78d6e73b04143b08aaf5147b544499be to your computer and use it in GitHub Desktop.
Save rseon/78d6e73b04143b08aaf5147b544499be to your computer and use it in GitHub Desktop.
[PHP] Search value in multidimensional array
<?php
/**
* Search value through multi dimensional array by column key and return first found sub array or corresponding key.
* This method preserves the original array keys.
*
* Multiple returns :
* - The sub-array if value is found
* - A mixed value corresponding to the key (if $onlyKey is set to true)
* - false if an error occured or value is not found
*
* A warning is triggered if :
* - Searched key does not exist in array
* - Searched key is not present in all sub-array
*
* @param mixed $columnKey The searched column name in array (type sensitive)
* @param mixed $needle The searched value
* @param array $array The array
* @param bool $onlyKey If true the function will returns only the corresponding key
* @param bool $strict If true the function will search for identical elements in the haystack
*
* @return false|mixed|array
*/
function get_by_key_value($columnKey, $needle, array $haystack, bool $onlyKey = false, bool $strict = false)
{
// The value can't be found in empty array
if(empty($haystack)) {
return false;
}
// Retrieve only the values of $columnKey from $array
$columns = array_column($haystack, $columnKey);
if(empty($columns)) {
trigger_error("get_by_key_value() : The column key {$columnKey} does not exist in the array", E_USER_WARNING);
return false;
}
// Set the real keys of $array
$keys = array_keys($haystack);
if(count($keys) !== count($columns)) {
trigger_error("get_by_key_value() : The keys in haystack must be the same", E_USER_WARNING);
return false;
}
$correctKeys = array_combine($keys, $columns);
// Searches in correct keys for the given value and returns the first corresponding key if successful
$found = array_search($needle, $correctKeys, $strict);
// Returns the key
if($onlyKey) {
return $found;
}
// Returns the data or false if not found
if($found === false) {
return false;
}
return $haystack[$found];
}
/**
* This methods is similar to get_by_key_value but returns an array of all found sub arrays or corresponding keys.
* This method preserves the original array keys.
*
* Multiple returns :
* - An array of sub-arrays if value are found
* - An array of mixed values corresponding to the keys (if $onlyKey is set to true)
* - An empty array if an error occured or value is not found
*
* A warning is triggered if :
* - Searched key does not exist in array
* - Searched key is not present in all sub-array
*
* @param mixed $columnKey The searched column name in array (type sensitive)
* @param mixed $needle The searched value
* @param array $array The array
* @param bool $onlyKey If true the function will returns only the corresponding key
* @param bool $strict If true the function will search for identical elements in the haystack
* @param bool $baseHaystack *Only for recursion, do not use*
* @param bool $keys *Only for recursion, do not use*
*
* @return array
*/
function get_by_key_value_multiple($columnKey, $needle, array $haystack, bool $onlyKey = false, bool $strict = false, array $baseHaystack = [], array $keys = [])
{
if(empty($baseHaystack)) {
$baseHaystack = $haystack;
}
$copy = $haystack;
$found = get_by_key_value($columnKey, $needle, $copy, true, $strict);
if($found === false) {
if($onlyKey) {
return $keys;
}
$result = [];
foreach($keys as $key) {
$result[$key] = $baseHaystack[$key];
}
return $result;
}
$keys[] = $found;
unset($copy[$found]);
return get_by_key_value_multiple($columnKey, $needle, $copy, $onlyKey, $strict, $baseHaystack, $keys);
}
// Tests
$haystack = [
'a' => [
'keyA' => 'Value A',
10 => 42,
14 => 'Value C',
'keyB' => 'Value B',
],
12 => [
'keyA' => 'Value A12',
10 => '54',
15 => 'Value D',
'keyB' => 'Value B',
],
];
var_dump(get_by_key_value('keyA', 'Value A12', $haystack)); // === $haystack[12]
var_dump(get_by_key_value(10, 42, $haystack)); // === $haystack['a']
var_dump(get_by_key_value(10, '42', $haystack, true)); // === 'a'
var_dump(get_by_key_value('10', '42', $haystack, false, true)); // === false (strict mode)
var_dump(get_by_key_value(15, 'Value D', $haystack)); // === false + Warning : 15 does not exists in $haystack[12]
// Multiple values attempted
var_dump(get_by_key_value('keyB', 'Value B', $haystack)); // === $haystack['a'] because is the first occurence
var_dump(get_by_key_value_multiple('keyB', 'Value B', $haystack)); // === array[$haystack['a'], $haystack[12]]
var_dump(get_by_key_value_multiple('keyB', 'Value C', $haystack)); // === empty array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment