Last active
November 26, 2019 10:14
-
-
Save raazon/b39d782e4908b6bf5f359c96ff7b8839 to your computer and use it in GitHub Desktop.
PHP multidimensional array search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* PHP multidimensional array search ( LIKE SEARCH ) | |
* @since 1.0.0 | |
*/ | |
function searchInMultidimensionalArray($value=NULL, $items=array()){ | |
$getItems = []; | |
foreach ($items as $item_key => $itemDatas) { | |
if(is_array($itemDatas)){ | |
foreach ($itemDatas as $dsdf =>$itemData) { | |
if (is_array($itemData)) { | |
foreach ($itemData as $innerItems) { | |
if (strpos(strtolower($innerItems), strtolower($value)) !== false || strpos(strtolower($value), strtolower($innerItems)) !== false) { | |
array_push($getItems, $item_key); | |
} | |
} | |
} else{ | |
if( strstr($itemData, $value) !== false || strstr($itemData, strtolower($value)) !== false || strstr($itemData, strtoupper($value)) !== false ){ | |
array_push($getItems, $item_key); | |
} | |
} | |
} | |
}else{ | |
array_push($getItems, 0); | |
} | |
} | |
return array_unique($getItems); | |
} | |
// Multidimensional (Three dimensional) array | |
$multidimensional_array = array( | |
"school1" => array( | |
"year" => "2017", | |
"data" => array( | |
'score' => '100', | |
'name' => 'Sam', | |
'subject' => 'Data Structures' | |
) | |
), | |
"school2" => array( | |
"year" => "2018", | |
"data" => array( | |
'score' => '50', | |
'name' => 'Tanya', | |
'subject' => 'Advanced Algorithms' | |
) | |
), | |
"school3" => array( | |
"year" => "2018", | |
"data" => array( | |
'score' => '75', | |
'name' => 'Jack', | |
'subject' => 'Distributed Computing' | |
) | |
) | |
); | |
print_r(searchInArray('sam', $multidimensional_array)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment