Iterating over a PHP associative array by key

Have a dream? Start learning your way toward it with courses from $12.99. Shop now for extra savings1px

The PHP scripting language provides associative arrays that allow one to associate a key in the array to a value; each key in the array must be unique. E.g., I can create an array of United States presidents that uses the presidents' names as the keys and their political party affiliation as the value for each key. I could then iterate through the array by key and show the corresponding value for each key as in the example PHP code below.



<?php

$presidents_party = array(
   "George Washingon" => "Unaffiliated",
   "John Adams" => "Federalist",
   "Thomas Jefferson" => "Democratic-Republican",
   "James Madison" => "Democratic-Republican",
   "James Monroe" => "Democratic-Republican",
   "John Quincy Adams" => "Democratic-Republican (1809-1828)/National Republican (1828-1830)",
   "Andrew Jackson" => "Democratic",
   "Martin Van Buren" => "Democratic",
   "William Henry Harrison" => "Whig",
   "John Tyler" => "Whig until 1841 when he was expelled from the party",
   "James K. Polk" => "Democratic",
   "Zachary Taylor" => "Whig",
   "Millard Fillmore" => "Whig",
   "Franklin Pierce" => "Democratic",
   "James Buchanan" => "Democratic",
   "Abraham Lincoln" => "Republican (1861-1864)/National Union (1864-1865)",
   "Andrew Johnson" => "National Union (1864-1868)/Democratic (1868-1875)",
   "Ulysses S. Grant" => "Republican",
   "Rutherford B. Hayes" => "Republican",
   "James A. Garfield" => "Republican",
   "Chester A. Arthur" => "Republican",
   "Grover Cleveland (1st term)" => "Democratic",
   "Benjamin Harrison" => "Republican",
   "Grover Cleveland (2nd term)" => "Democratic",
   "William McKinley" => "Republican",
   "Theodore Roosevelt" => "Republican",
   "William Howard Taft" => "Republican",
   "Woodrow Wilson" => "Democratic",
   "William G. Harding" => "Republican",
   "Calvin Coolidge" => "Republican",
   "Herbert Hoover" => "Republican",
   "Franklin D. Roosevelt" => "Democratic",
   "Harry S. Truman" => "Democratic",
   "Dwight D. Eisenhower" => "Republican",
   "John F. Kennedy" => "Democratic",
   "Lyndon B. Johnson" => "Democratic",
   "Richard Nixon" => "Republican",
   "Gerald Ford" => "Republican",
   "Jimmy Carter" => "Democratic",
   "Ronald Reagan" => "Republican",
   "George H. W. Bush" => "Republican",
   "Bill Clinton" => "Democratic",
   "George W. Bush" => "Republican",
   "Barack Obama" => "Democratic",
   "Donald Trump" => "Republican",
   "Joseph Biden" => "Democratic"
);

$i = 1;
foreach ($presidents_party as $president => $party ) {
  echo $i++ . ". " . $president . ": " . $party . "<br>";
}

?>

In the example above, I also wish to display a number for each president's sequence of holding the position. E.g., George Washington was the first president and Joseph Biden is the 46th president. Since Grover Cleveland held the office twice, before and after Benjamin Harrison, I need some way to distinguish his first term from his second term, since if I'm using presidential names as keys in the array, only one instance can appear in the array. I use the variable $i to track presidents' sequence in the list of presidents of the United States so display its value with the echo command and then increment it by one with $i++. If I didn't add the term number in the key for each instance of Grover Cleveland's name, President Biden would be shown as the 45th president rather than the 46th president since Grover Cleveland's name can only serve as one key.

The php foreach command will traverse the array $presidents_party displaying the keys one by one followed by the value for each key which I assign to the variable $party. The output of the code can be seen at United States Presidents' Political Parties.

References:

  1. PHP foreach Loop
    W3Schools
  2. How to properly loop through array keys in PHP [closed]
    Date: November 16, 2017
    Stack Overflow