Created
March 16, 2022 01:19
-
-
Save mdestafadilah/a5fd0054dd1bcfcfa056d40c4909dca3 to your computer and use it in GitHub Desktop.
Kolom array yang tidak ada di PHP 5.4
This file contains 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
# source: https://stackoverflow.com/a/33919648 | |
# using for php < 5.4 if server using > 5.5 please, use array_columnt instead | |
if (!function_exists('arr_kolom')) { | |
function arr_kolom(array $array, $columnKey, $indexKey = null) | |
{ | |
$result = array(); | |
foreach ($array as $subArray) { | |
if (!is_array($subArray)) { | |
continue; | |
} elseif (is_null($indexKey) && array_key_exists($columnKey, $subArray)) { | |
$result[] = $subArray[$columnKey]; | |
} elseif (array_key_exists($indexKey, $subArray)) { | |
if (is_null($columnKey)) { | |
$result[$subArray[$indexKey]] = $subArray; | |
} elseif (array_key_exists($columnKey, $subArray)) { | |
$result[$subArray[$indexKey]] = $subArray[$columnKey]; | |
} | |
} | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment