Home  >  Article  >  Backend Development  >  PHP recursion infinite classification and return array example

PHP recursion infinite classification and return array example

WBOY
WBOYOriginal
2016-07-25 09:04:461268browse
  1. /**

  2. * Recursive infinite classification returns array
  3. * link: bbs.it-home.org
  4. * date: 2013/2/21
  5. */
  6. $conn = mysql_connect('localhost','root','123456');
  7. mysql_select_db('test');
  8. mysql_query("set names 'utf8'");

  9. function getCate($pid = 0)

  10. {
  11. $sql = "select * from cate where pid=".$pid;
  12. $res = mysql_query($sql);
  13. if($res)
  14. {
  15. while($row = mysql_fetch_assoc($res)){
  16. $categories[] = $row;
  17. }
  18. }

  19. < ;p>if(0 < count($categories))
  20. {
  21. for($i = 0; $i < count($categories); $i++)
  22. {
  23. $categories[$i]['child' ] = getCate($categories[$i]['id']);
  24. }

  25. }

  26. return $categories;

  27. }
  28. //Call

  29. $cate = getCate(0);
  30. ?>

Copy code
Articles you may be interested in: php infinite classification recursive function Extended code for php to implement infinite-level caching classes Unlimited scalable menu made with php and js Unlimited classification implemented by php and mysql php generates an array class from infinite classification Analysis of infinite classification in ecshop Infinite level selectTree class written in php php uses recursive method to implement infinite classification code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn