Php multi-dimensional array quickly queries the parent key and parent key value based on the key name

Source: Internet
Author: User
Tags createindex
Php multi-dimensional array quickly queries the parent key and parent key value based on the key name

  1. /**
  2. * FILE_NAME: arr. php FILE_PATH: test/
  3. * Quick query of the parent key and parent key value based on the key name in a multi-dimensional array
  4. *
  5. * @ Copyright Copyright (c) 2006-2010
  6. * @ Author Levi
  7. * @ Package test. arr
  8. * @ Subpackage
  9. * @ Version 2011-04-29
  10. * @ Link bbs.it-home.org
  11. */
  12. Header ("Content-Type: text/html; charset = utf-8 ");
  13. $ Arr = array
  14. (
  15. 'China' => array
  16. (
  17. 'Name' => 'China ',
  18. 'Cite '=> array
  19. (
  20. 'Beijing' => array
  21. (
  22. 'Name' => 'Beijing ',
  23. 'Site' => array ('chunyang' => 'chaoyang District ', 'xuanwu' => 'xuanwu District ')
  24. ),
  25. 'Shanghai' => array
  26. (
  27. 'Name' => 'Shanghai ',
  28. 'Site' => array ('jinganc' => 'jing 'an District ', 'huangpu' => 'huangpu District ')
  29. )
  30. )
  31. )
  32. );
  33. Function printA ($ data)
  34. {
  35. Echo'
    ';
  36. print_r($data);
  37. echo '
  38. ';
  39. }
  40. Function indexKey ($ data, $ parent = NULL)
  41. {
  42. $ Arr = array ();
  43. Foreach ($ data as $ key => $ value)
  44. {
  45. $ Arr [$ key] = $ parent;
  46. If (is_array ($ value ))
  47. {
  48. $ Arr + = indexKey ($ value, $ key );
  49. }
  50. }
  51. Return (Array) $ arr;
  52. }
  53. PrintA (indexKey ($ arr ));
  54. ?>

Print the following data: Array ([china] => [name] => china [cite] => china [beijing] => cite [site] => beijing [chaoyang] => site [xuanwu] => site [shanghai] => cite [jingan] => site [huangpu] => site) but there is a problem with writing like this: if there is a key with the same name, it will lead to loss, so I write such a class and only need to pass the array to the object, the object provides two interfaces: printArr printing index array search query key name parent array key name IndexKey creation query index query class:

  1. /**
  2. * FILE_NAME: arr. php FILE_PATH: test/
  3. * Quick query of the parent key and parent key value based on the key name in a multi-dimensional array
  4. *
  5. * @ Copyright Copyright (c) 2006-2010
  6. * @ Author Levi
  7. * @ Package test. arr
  8. * @ Subpackage
  9. * @ Version 2011-04-29
  10. * @ Link bbs.it-home.org
  11. */
  12. Header ("Content-Type: text/html; charset = utf-8 ");
  13. $ Arr = array
  14. (
  15. 'China' => array
  16. (
  17. 'Name' => 'China ',
  18. 'Cite '=> array
  19. (
  20. 'Beijing' => array
  21. (
  22. 'Name' => 'Beijing ',
  23. 'Site' => array ('chunyang' => 'chaoyang District ', 'xuanwu' => 'xuanwu District ')
  24. ),
  25. 'Shanghai' => array
  26. (
  27. 'Name' => 'Shanghai ',
  28. 'Site' => array ('jinganc' => 'jing 'an District ', 'huangpu' => 'huangpu District ')
  29. )
  30. )
  31. )
  32. );
  33. Function printA ($ data)
  34. {
  35. Echo'
    ';
  36. print_r($data);
  37. echo '
  38. ';
  39. }
  40. Function printP (IndexKey $ obj, $ key)
  41. {
  42. $ Parent = $ obj-> search ($ key );
  43. If ($ parent)
  44. {
  45. Echo '"'. $ key. '" Parent Key is :';
  46. If (! Is_array ($ parent ))
  47. {
  48. Echo $ parent ."
    \ N ";
  49. }
  50. Else printA ($ parent );
  51. }
  52. Else echo 'no Parent or no Search of "'. $ key .'"! '."

    \ N ";
  53. }
  54. Class IndexKey
  55. {
  56. Private $ _ arr = array ();
  57. Public function _ construct ($ data)
  58. {
  59. $ This-> _ createIndex ($ data );
  60. }
  61. Public function printArr ()
  62. {
  63. Return (Array) $ this-> _ arr;
  64. }
  65. Public function search ($ key)
  66. {
  67. Return isset ($ this-> _ arr [$ key])? $ This-> _ arr [$ key]: NULL;
  68. }
  69. Private function _ createIndex ($ data, $ parent = NULL)
  70. {
  71. Foreach ($ data as $ key => $ value)
  72. {
  73. $ This-> _ checkIndex ($ key, $ parent );
  74. If (is_array ($ value ))
  75. {
  76. $ This-> _ createIndex ($ value, $ key );
  77. }
  78. }
  79. }
  80. Private function _ checkIndex ($ key, $ parent)
  81. {
  82. $ Index = isset ($ this-> _ arr [$ key])? $ This-> _ arr [$ key]: NULL;
  83. If ($ index)
  84. {
  85. If (is_array ($ index ))
  86. {
  87. Array_push ($ this-> _ arr [$ key], $ parent );
  88. }
  89. Else $ this-> _ arr [$ key] = array ($ index, $ parent );
  90. }
  91. Else $ this-> _ arr [$ key] = $ parent;
  92. }
  93. }
  94. $ Index = (Object) new IndexKey ($ arr );
  95. PrintA ($ index-> printArr ());
  96. PrintP ($ index, 'Beijing ');
  97. PrintP ($ index, 'name ');
  98. PrintP ($ index, 'China ');
  99. ?>

At last, only one data output is missing, so I modified this class and provided three external methods: printArr print index array search query key name parent array key name parentValue query parent key value

  1. /**
  2. * FILE_NAME: arr. php FILE_PATH: test/
  3. * Quick query of the parent key and parent key value based on the key name in a multi-dimensional array
  4. *
  5. * @ Copyright Copyright (c) 2006-2010
  6. * @ Author Levi
  7. * @ Package test. arr
  8. * @ Subpackage
  9. * @ Version 2011-04-29
  10. * @ Link bbs.it-home.org
  11. */
  12. Header ("Content-Type: text/html; charset = utf-8 ");
  13. $ Arr = array
  14. (
  15. 'China' => array
  16. (
  17. 'Name' => 'China ',
  18. 'Cite '=> array
  19. (
  20. 'Beijing' => array
  21. (
  22. 'Name' => 'Beijing ',
  23. 'Site' => array ('chunyang' => 'chaoyang District ', 'xuanwu' => 'xuanwu District ')
  24. ),
  25. 'Shanghai' => array
  26. (
  27. 'Name' => 'Shanghai ',
  28. 'Site' => array ('jinganc' => 'jing 'an District ', 'huangpu' => 'huangpu District ')
  29. )
  30. )
  31. )
  32. );
  33. Function printA ($ data)
  34. {
  35. Echo'
    ';
  36. print_r($data);
  37. echo '
  38. ';
  39. }
  40. Function printP2 (IndexArr $ obj, $ key)
  41. {
  42. $ Parent = $ obj-> search ($ key );
  43. If (! Is_array ($ parent ))
  44. {
  45. If ($ parent)
  46. {
  47. Echo '"'. $ key. '" Parent Key is:'. $ parent ."
    \ N ";
  48. }
  49. Else echo 'no Parent or no Search of "'. $ key .'"! '."
    \ N ";;
  50. Echo '"'. $ key. '" Parent "'. $ parent. '" Value is :';
  51. PrintA ($ obj-> parentValue ($ key ));
  52. }
  53. Else printA ($ parent );
  54. }
  55. Class IndexArr
  56. {
  57. Private $ _ arr = array ();
  58. Public function _ construct ($ data)
  59. {
  60. $ This-> _ createIndex ($ data );
  61. }
  62. Public function printArr ()
  63. {
  64. Return (Array) $ this-> _ arr;
  65. }
  66. Public function search ($ key)
  67. {
  68. Return isset ($ this-> _ arr [$ key])? $ This-> _ arr [$ key] ['parent']: NULL;
  69. }
  70. Public function parentValue ($ key)
  71. {
  72. Return isset ($ this-> _ arr [$ key])? $ This-> _ arr [$ key] ['data']: NULL;
  73. }
  74. Private function _ createIndex ($ data, $ parent = NULL)
  75. {
  76. Foreach ($ data as $ key => $ value)
  77. {
  78. $ This-> _ checkIndex ($ key, $ parent, $ data );
  79. If (is_array ($ value ))
  80. {
  81. $ This-> _ createIndex ($ value, $ key );
  82. }
  83. }
  84. }
  85. Private function _ checkIndex ($ key, $ parent, $ data)
  86. {
  87. $ Data = $ parent & isset ($ data [$ parent])? $ Data [$ parent]: $ data;
  88. ! Isset ($ this-> _ arr [$ key]) & $ this-> _ arr [$ key] = array ('data' => $ data, 'parent' => '');
  89. $ Index = & $ this-> _ arr [$ key] ['parent'];
  90. If (! Empty ($ index ))
  91. {
  92. If (is_array ($ index ))
  93. {
  94. Array_push ($ index, $ parent );
  95. }
  96. Else $ index = array ($ index, $ parent );
  97. }
  98. Else $ index = $ parent;
  99. }
  100. }
  101. $ Index2 = (Object) new IndexArr ($ arr );
  102. PrintA ($ index2-> printArr ());
  103. PrintP2 ($ index2, 'Beijing ');
  104. PrintP2 ($ index2, 'name ');
  105. PrintP2 ($ index2, 'China ');
  106. ?>

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.