Get column of last names from a recordset, indexed by the "id" column:


SUBMITTED BY: henry1874w

DATE: June 22, 2017, 12:10 a.m.

FORMAT: Text only

SIZE: 539 Bytes

HITS: 291

  1. <?php
  2. // An array that represents a possible record set
  3. returned from a database
  4. $a = array(
  5. array(
  6. 'id' => 5698,
  7. 'first_name' => 'Peter',
  8. 'last_name' => 'Griffin',
  9. ),
  10. array(
  11. 'id' => 4767,
  12. 'first_name' => 'Ben',
  13. 'last_name' => 'Smith',
  14. ),
  15. array(
  16. 'id' => 3809,
  17. 'first_name' => 'Joe',
  18. 'last_name' => 'Doe',
  19. )
  20. );
  21. $last_names = array_column($a, 'last_name', 'id');
  22. print_r($last_names);
  23. ?>
  24. Output:
  25. Array
  26. (
  27. [5698] => Griffin
  28. [4767] => Smith
  29. [3809] => Doe
  30. )

comments powered by Disqus