Home  >  Article  >  Backend Development  >  PHP multi-dimensional array quickly queries its parent key and parent key value based on the key name

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

WBOY
WBOYOriginal
2016-07-25 09:04:271452browse
  1. /**
  2. * FILE_NAME : arr.php FILE_PATH : test/
  3. * Quickly query the parent key and parent key value according to 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('chaoyang' => 'Chaoyang District', 'xuanwu' => 'Xuanwu District')
  24. ),
  25. 'shanghai' => array
  26. (
  27. 'name' => 'Shanghai ',
  28. 'site' => array('jingan' => 'Jing'an District', 'huangpu' => 'Huangpu District')
  29. )
  30. )
  31. )
  32. );
  33. function printA($data)
  34. {
  35. echo '
    ';</li>
    <li>print_r($data);</li>
    <li>echo '
    ';
  36. }
  37. function indexKey($data, $parent = NULL)
  38. {
  39. $arr = array() ;
  40. foreach ($data as $key => $value)
  41. {
  42. $arr[$key] = $parent;
  43. if (is_array($value))
  44. {
  45. $arr += indexKey($value, $ key);
  46. }
  47. }
  48. return (Array)$arr;
  49. }
  50. printA(indexKey($arr));
  51. ?>
Copy the code

Print the data as follows Array ( [china] => [name] => china [cite] => china [beijing] => cite [site] => beijing [chaoyang] => site [xuanwu] => site [shanghai] => cite [jingan] => site [huangpu] => site ) However, there is a problem with writing the above, that is: if there is a key with the same name, it will be lost, so I wrote such a class Just pass the array to the object, the object provides two interfaces printArr prints index array search queries the parent array key name of the key name IndexKey creates query index query class:

  1. /**
  2. * FILE_NAME : arr.php FILE_PATH : test/
  3. * Quickly query the parent key and parent key value according to 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('chaoyang' => 'Chaoyang District', 'xuanwu' => 'Xuanwu District')
  24. ),
  25. 'shanghai' => array
  26. (
  27. 'name' => ' Shanghai',
  28. 'site' => array('jingan' => 'Jing'an District', 'huangpu' => 'Huangpu District')
  29. )
  30. )
  31. )
  32. );
  33. function printA($data)
  34. {
  35. echo '
    ';</li>
    <li>print_r($data);</li>
    <li>echo '
    ';
  36. }
  37. function printP(IndexKey $obj, $key)
  38. {
  39. $parent = $obj- >search($key);
  40. if ($parent)
  41. {
  42. echo '"'.$key.'" Parent Key is: ';
  43. if (!is_array($parent))
  44. {
  45. echo $parent. "
    n";
  46. }
  47. else printA($parent);
  48. }
  49. else echo 'NO Parent OR No Search of "'.$key.'"!'."

    n";
  50. }
  51. class IndexKey
  52. {
  53. private $_arr = array();
  54. public function __construct($data)
  55. {
  56. $this->_createIndex($data);
  57. }
  58. public function printArr()
  59. {
  60. return (Array)$this->_arr;
  61. }
  62. public function search($key)
  63. {
  64. return isset($this->_arr[$key]) ? $this- >_arr[$key] : NULL;
  65. }
  66. private function _createIndex($data, $parent = NULL)
  67. {
  68. foreach ($data as $key => $value)
  69. {
  70. $this->_checkIndex ($key, $parent);
  71. if (is_array($value))
  72. {
  73. $this->_createIndex($value, $key);
  74. }
  75. }
  76. }
  77. private function _checkIndex($key, $parent )
  78. {
  79. $index = isset($this->_arr[$key]) ? $this->_arr[$key] : NULL;
  80. if ($index)
  81. {
  82. if (is_array($index) )
  83. {
  84. array_push($this->_arr[$key], $parent);
  85. }
  86. else $this->_arr[$key] = array($index, $parent);
  87. }
  88. else $ this->_arr[$key] = $parent;
  89. }
  90. }
  91. $index = (Object)new IndexKey($arr);
  92. printA($index->printArr());
  93. printP($index, 'beijing');
  94. printP($index, 'name');
  95. printP($index, 'china');
  96. ?>
Copy code

In the end, there was only one data output left, so I modified this class Provides three external methods printArr prints index array search queries the parent array key name of the key name parentValue queries the parent key value

  1. /**
  2. * FILE_NAME : arr.php FILE_PATH : test/
  3. * Quickly query the parent key and parent key value according to 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('chaoyang' => 'Chaoyang District', 'xuanwu' => 'Xuanwu District')
  24. ),
  25. 'shanghai' => array
  26. (
  27. 'name' => ' Shanghai',
  28. 'site' => array('jingan' => 'Jing'an District', 'huangpu' => 'Huangpu District')
  29. )
  30. )
  31. )
  32. );
  33. function printA($data)
  34. {
  35. echo '
    ';</li>
    <li>print_r($data);</li>
    <li>echo '
    ';
  36. }
  37. function printP2(IndexArr $obj, $key)
  38. {
  39. $parent = $obj- >search($key);
  40. if (!is_array($parent))
  41. {
  42. if ($parent)
  43. {
  44. echo '"'.$key.'" Parent Key is: '.$parent."< ;br />n";
  45. }
  46. else echo 'NO Parent OR No Search of "'.$key.'"!'."
    n";;
  47. echo '"'.$key .'" Parent "'.$parent.'" Value is: ';
  48. printA($obj->parentValue($key));
  49. }
  50. else printA($parent);
  51. }
  52. class IndexArr
  53. {
  54. private $_arr = array();
  55. public function __construct($data)
  56. {
  57. $this->_createIndex($data);
  58. }
  59. public function printArr()
  60. {
  61. return (Array)$this-> _arr;
  62. }
  63. public function search($key)
  64. {
  65. return isset($this->_arr[$key]) ? $this->_arr[$key]['parent'] : NULL;
  66. }
  67. public function parentValue($key)
  68. {
  69. return isset($this->_arr[$key]) ? $this->_arr[$key]['data'] : NULL;
  70. }
  71. private function _createIndex ($data, $parent = NULL)
  72. {
  73. foreach ($data as $key => $value)
  74. {
  75. $this->_checkIndex($key, $parent, $data);
  76. if (is_array( $value))
  77. {
  78. $this->_createIndex($value, $key);
  79. }
  80. }
  81. }
  82. private function _checkIndex($key, $parent, $data)
  83. {
  84. $data = $parent & isset($data[$parent]) ? $data[$parent] : $data;
  85. !isset($this->_arr[$key]) & $this->_arr[$key] = array(' data' => $data, 'parent' => '');
  86. $index = &$this->_arr[$key]['parent'];
  87. if (!empty($index))
  88. {
  89. if (is_array($index))
  90. {
  91. array_push($index, $parent);
  92. }
  93. else $index = array($index, $parent);
  94. }
  95. else $index = $parent;
  96. }
  97. }
  98. $index2 = (Object)new IndexArr($arr);
  99. printA($index2->printArr());
  100. printP2($index2, 'beijing');
  101. printP2($index2, 'name') ;
  102. printP2($index2, 'china');
  103. ?>
Copy 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