13 June 2024

Using this solution we find and display the item from an array of associative arrays that has any specified attribute (like title, name, etc.) which comes first alphabetically. This approach can be applied to various datasets where sorting by a specific attribute is needed to identify the leading item.

Source code viewer
  1. // Sample array of associative arrays.
  2. $items = [
  3. ["title" => "Banana", "value" => "Yellow"],
  4. ["title" => "Apple", "value" => "Green"],
  5. ["title" => "Grape", "value" => "Purple"],
  6. ["title" => "Cherry", "value" => "Red"],
  7. ["title" => "Date", "value" => "Brown"]
  8. ];
  9.  
  10. // Extract the titles and sort them alphabetically.
  11. $titles = array_column($items, 'title');
  12. sort($titles);
  13.  
  14. // Get the first title alphabetically.
  15. $firstTitle = $titles[0];
Programming Language: PHP