Hoping I am not opening a duplicate question but I didn't see this type of question being asked or the answers I saw didn't seem to work with my dataset (but I'm very new at this).
I am hoping to sort the following data set (array of arrays) based on another array of strings.
The code is running and it is only running the generateHtml function for strings that exist in the $ntbooks array which is by design. What I need to solve is how to perform a custom sort using the $ntbooks as the order. Right now, as the code runs and the content is parsed from content.php, the order the data is populated into the menu is the order in which it is seen (top to bottom of array)--I would like to sort the data so that Matthew is written out, then Mark, then Luke, etc.
This is a sample of my data but there are over 250 arrays (contentids) in it and I would like to avoid restructuring or reorganizing my dataset. When all is said and done, the generateHTML should start with matthew, mark, luke, and work its way through the list.
Thank you in advance for your help!
Dynamically Generated HTML Menu (menu.php):
include('content.php');
$main = array();
foreach($articles as $article)
{
foreach(explode("; ", $article["verse"]) as $verseData)
{
$verse = explode(" ", $verseData);
$book = $verse[0];
$verse = explode(":", $verse[1]);
$chapter = $verse[0];
if(empty($main[$book]))
$main[$book] = array();
if(empty($main[$book][$chapter]))
$main[$book][$chapter] = array();
if(empty($main[$book][$chapter][$verse[1]]))
$main[$book][$chapter][$verse[1]] = array();
$main[$book][$chapter][$verse[1]] = array
(
"full" => $article["full"]
);
}
}
$ntbooks = array("Matthew", "Mark", "Luke", "John", "Acts", "Romans", "1Corinthians", "2Corinthians", "Galatians", "Ephesians", "Philippians", "Colossians", "1Thessalonians", "2Thessalonians", "1Timothy", "2Timothy", "Titus", "Philemon", "Hebrews", "James", "1Peter", "2Peter", "1John", "2John", "3John", "Jude", "Revelation");
foreach($main as $book => $data)
{
if (in_array($book, $ntbooks)) {
generateHtml($book, $data);
}
}
function generateHtml($book, $data)
{
echo "<!-- ". $book ." -->\n";
echo "<div class=\"submenu\">\n";
echo "<a href=\"#\">". $book ."</a>\n";
echo "<!-- Level 2 menu -->\n";
echo "<div>\n";
echo "<div>\n";
foreach($data as $chapter => $verses)
{
echo "<div class=\"submenu\">\n";
echo "<a href=\"#\"">Chapter ". $chapter ."</a>\n";
echo "<!-- Level 3 menu -->\n";
echo "<div>\n";
echo "<div>\n";
foreach($verses as $verse => $value)
{
echo "<a href=\"#\"">Verse ". $verse ."</a>\n";
}
echo "</div>\n";
echo "</div>\n";
echo "</div>\n";
}
}
PHP Array of Arrays (content.php):
$articles = array(
array(
'contentid' => '0',
'full' => 'Item1 by Artist Name1 (Matthew 4)',
'verse' => 'Matthew 4:18-22',
'commentary' => ''),
array(
'contentid' => '1',
'full' => 'Item2 by Artist Name2 (Luke 15)',
'verse' => 'Luke 15:11-14; Luke 15:20-21',
'commentary' => ''),
array(
'contentid' => '2',
'full' => 'Item3 by Artist Name3 (John 3)',
'verse' => 'John 3:1-9',
'commentary' => ''),
array(
'contentid' => '3',
'full' => 'Item4 by Artist Name4 (John 8)',
'verse' => 'John 8:1-15A',
'commentary' => ''),
array(
'contentid' => '4',
'full' => 'Item5 by Artist Name5 (Matthew 27; Luke 23; John 19)',
'verse' => 'Matthew 27:20-23A; Luke 23:20-25A; John 19:2-3A',
'commentary' => 'Here '));