Skip to content

Instantly share code, notes, and snippets.

@arvindsvt
Created February 5, 2018 15:33
Show Gist options
  • Save arvindsvt/b30334b9d9f38cb46cce9c6d95c4abb3 to your computer and use it in GitHub Desktop.
Save arvindsvt/b30334b9d9f38cb46cce9c6d95c4abb3 to your computer and use it in GitHub Desktop.
<?php
function multidimensional_array_search($search_value,$array) {
$mached = array();
if(is_array($array) && count($array) > 0) {
foreach($array as $key => $value) {
if(is_array($value) && count($value) > 0) {
multidimensional_array_search($search_value,$value);
} else {
return array_search($search_value,$array); exit;
}
}
}
}
// useage
echo multidimensional_array_search('test3',array('test','test1','test2','test3'));
function recursiveArraySearch($haystack, $needle, $index = null)
{
$aIt = new RecursiveArrayIterator($haystack);
$it = new RecursiveIteratorIterator($aIt);
while($it-&gt;valid())
{
if (((isset($index) AND ($it-&gt;key() == $index)) OR (!isset($index))) AND ($it-&gt;current() == $needle)) {
return $aIt-&gt;key();
}
$it-&gt;next();
}
return false;
}
?>
Removing specific value Using array_diff()
function remove_element($array,$value) {
return array_diff($array, (is_array($value) ? $value : array($value)));
}
$myArray = array('php', 'laravel', '.net', 'java', 'c#', 'javascript');
$result = remove_element($myArray,'.net');
print_r($result);
array_map
function my_array_map($function, $arrary)
{
$result = array();
foreach ($arr as $key => $val)
{
$result[$key] = (is_array($val) ? my_array_map($function, $val) : $function($val));
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment