How eval works

Source: Internet
Author: User

Reference: http://www.phpchina.com/bbs/thread-25541-1-15.html

 

In this example, Eval is used to store array elements on a pay-as-you-go basis for unlimited classification.

 

  1. Class m_layer {
  2. /**
  3. * Unlimited Classification
  4. *
  5. * @ Param object $ MDB
  6. * @ Param string $ table
  7. * @ Param string $ label_colum
  8. * @ Return Array
  9. */
  10. Function get_layer ($ layer, $ label_colum = 'label '){
  11. $ New_layer = array ();
  12. If (! Is_array ($ layer) |! Is_array (current ($ layer) |! Array_key_exists ($ label_colum, current ($ layer ))){
  13. Return array ();
  14. }
  15. Foreach ($ layer as $ row ){
  16. $ Row_str = '';
  17. Foreach ($ row as $ key => $ value ){
  18. $ Row_str. = ", '$ key' =>' $ value '";
  19. }
  20. $ Row_str = "array (". substr ($ row_str, 1 ).')';
  21. $ Label_arr = explode ('-', $ row [$ label_colum]);
  22. $ Layer_str = '$ new_layer ';
  23. Foreach ($ label_arr as $ label ){
  24. If (intval ($ label )){
  25. $ Layer_str. = "['child '] [' $ label']";
  26. }
  27. }
  28. Eval ("$ layer_str = $ row_str; echo strip_tags (/"/$ layer_str =/$ row_str /");");
  29. Echo '<br/> ';
  30. }
  31. Return $ new_layer ['child '];
  32. }
  33. /**
  34. * Getting a new layer is equivalent to mysql_insert_id.
  35. *
  36. * @ Param object $ MDB
  37. * @ Param string $ table
  38. * @ Param string $ father_label
  39. * @ Param string $ label_colum
  40. * @ Return Array
  41. */
  42. Function get_new_layer ($ mdb, $ table, $ father_label, $ label_colum = 'labe '){
  43. $ COUNT = $ father_label? Count (explode ('-', $ father_label): 0;
  44. $ Size = strlen ($ father_label );
  45. $ SQL = "select $ label_colum from $ table where left ($ label_colum, $ size) = '$ father_label' order by $ label_colum DESC ";
  46. $ Rs = $ MDB-> fetch_row ($ SQL );
  47. $ Last_label_arr = explode ('-', $ Rs [$ label_colum]);
  48. $ Last_label = '';
  49. For ($ I = 0; $ I <= $ count; $ I ++ ){
  50. $ Last_label. = '-'. $ last_label_arr [$ I];
  51. }
  52. $ Last_label = substr ($ last_label, 1 );
  53. $ New_label = explode ('-', $ last_label );
  54. $ Num = array_pop ($ new_label );
  55. $ New_label = implode ('-', $ new_label). '-'. ++ $ num;
  56. Return $ new_label;
  57. }
  58. }
  59. $ Label [] = array ('name' => 'family of games ', 'lay' => '01 ');
  60. $ Label [] = array ('name' => 'Online game', 'lay' => '01-01 ');
  61. $ Label [] = array ('name' => 'kart ', 'lay' => '01-01-01 ');
  62. $ Label [] = array ('name' => 'bubble thup', 'lay' => '01-01-02 ');
  63. $ Label [] = array ('name' => 'standalone game', 'lay' => '01-02 ');
  64. $ Label [] = array ('name' => 'resident evil ', 'lay' => '01-02-01 ');
  65. $ Label [] = array ('name' => 'death squads ', 'lay' => '01-02-02 ');
  66. $ Label [] = array ('name' => 'super Mary', 'lay' => '01-02-02 ');
  67. $ Label [] = array ('name' => 'entertainment & Leisure ', 'lay' => '02 ');
  68. $ Label [] = array ('name' => 'music home', 'lay' => '02-01 ');
  69. $ Label [] = array ('name' => 'Jay Chou ', 'lay' => '02-01-01 ');
  70. $ Label [] = array ('name' => 'Liu ruoying ', 'layer' => '02-01-02 ');
  71. $ Label [] = array ('name' => 'scholarly door', 'lay' => '02-02 ');
  72. $ Label [] = array ('name' => 'fiction ', 'lay' => '02-02-01 ');
  73. $ Label [] = array ('name' => 'novels ', 'lay' => '02-02-01-01 ');
  74. $ Label [] = array ('name' => 'short story ', 'lay' => '02-02-01-02 ');
  75. $ Label [] = array ('name' => 'journal ', 'lay' => '02-02-02 ');
  76. $ Layeray = m_layer: get_layer ($ label, 'layer ');
  77. Print_r ($ layeray );
  78. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.