/**
* Generate all the possible combinations among a set of nested arrays.
* @author fabiocicerchia
* @homepage https://gist.github.com/fabiocicerchia/4556892
*
* @param array $data The entrypoint array container.
* @param array $all The final container (used internally).
* @param array $group The sub container (used internally).
* @param mixed $val The value to append (used internally).
* @param int $i The key index (used internally).
*/
function generate_combinations(array $data, array &$all = array(), array $group = array(), $value = null, $i = 0)
{
$keys = array_keys($data);
if (isset($value) === true) {
array_push($group, $value);
}
if ($i >= count($data)) {
array_push($all, $group);
} else {
$currentKey = $keys[$i];
$currentElement = $data[$currentKey];
foreach ($currentElement as $val) {
generate_combinations($data, $all, $group, $val, $i + 1);
}
}
return $all;
}
$params = array(
'param1' => array(1,2,3),
'param2' => array(1,2,3),
'param3' => array(1,2,3)
);
foreach (array_values(generate_combinations($params)) as $args) {
$object = new Object($args[0], $args[1], $args[2]);
}
$search = array_filter($arr, function (array $item) use ($needle) {
foreach($needle as $key => $value) {
if (!isset($item[$key]) || $item[$key] !== $value) {
return false;
}
}
return true;
}
$search = array_filter($arr, function (array $item) use ($needle) {
return $needle == array_intersect_key($item, $needle);
}
$search = array_search($needle, array_map(function (array $item) use ($needle) {
return array_intersect_keys($item, $needle);
}, $arr, false),
$search = [];
foreach ($arr as $item) {
if ($needle == array_intersect_key($item, $needle)) {
$search[] = $item;
}
}