Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Comp function to sort PHP Array by Bible Verses (Scripture Books)
function scripturesort($a, $b) {
if ($a == $b) {return 0;}
$order=array("Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy", "Joshua", "Judges", "Ruth", "1 Samuel", "2 Samuel", "1 Kings", "2 Kings", "1 Chronicles", "2 Chronicles", "Ezra", "Nehemiah", "Esther", "Job", "Psalm", "Proverbs", "Ecclesiastes", "Song of Solomon", "Isaiah", "Jeremiah", "Lamentations", "Ezekiel", "Daniel", "Hosea", "Joel", "Amos", "Obadiah", "Jonah", "Micah", "Nahum", "Habakkuk", "Zephaniah", "Haggai", "Zechariah", "Malachi", "Matthew", "Mark", "Luke", "John", "Acts", "Romans", "1 Corinthians", "2 Corinthians", "Galatians", "Ephesians", "Philippians", "Colossians", "1 Thessalonians", "2 Thessalonians", "1 Timothy", "2 Timothy", "Titus", "Philemon", "Hebrews", "James", "1 Peter", "2 Peter", "1 John", "2 John", "3 John", "Jude", "Revelation");
$fa = $a;
$fb = $b;
preg_match('/^..(.+?)\b/', $a, $matches);
$a = trim(substr($matches[0],0,4));
preg_match('/^..(.+?)\b/', $b, $matches);
$b = trim(substr($matches[0],0,4));
similar_text($a, $b, $p);
if($p > 75) {
preg_match('/(.+?)([\d]+)/', $fa, $matches);
$na = intval($matches[2]);
preg_match('/(.+?)([\d]+)/', $fb, $matches);
$nb = intval($matches[2]);
if($na == $nb) {
preg_match('/(.+?)([\d]+).*?:([\d]+)/', $fa, $matches);
$n2a = intval($matches[3]);
preg_match('/(.+?)([\d]+).*?:([\d]+)/', $fb, $matches);
$n2b = intval($matches[3]);
return ($n2a < $n2b) ? -1 : 1;
} else
return ($na < $nb) ? -1 : 1;
}
$position = key(preg_grep('/^'.$a.'(.*)\b/', $order));
$position2 = key(preg_grep('/^'.$b.'(.*)\b/', $order));
if ($position2!==false && $position!==false) {return ($position < $position2) ? -1 : 1;}
if($position!==false) {return -1;}
if($position2!==false) {return 1;}
return ($a < $b) ? -1 : 1;
}
usort($scriptures, 'scripturesort');
@dajare

This comment has been minimized.

Copy link
Owner Author

@dajare dajare commented Mar 15, 2018

Sample array for input (n.b. typos are deliberate!):

Array
(
    [0] => Ruth
    [1] => Psalm 117
    [2] => Romans 16
    [3] => Matt 1:18-25
    [4] => Psalms 150
    [5] => Matthew 1:1-2:12
    [6] => Genesis 22
    [7] => Heb 11:1-3
    [8] => 1 Samuel 1
    [9] => 2 Thessalonians 2
    [10] => Genesis 3:15
    [11] => Zechariah 7
    [12] => John 17
    [13] => 2 Chrnicles 35
    [14] => John 21
    [15] => Acts 1
    [16] => Matthew 12
    [17] => Exodus 14-15
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment