Heim >Backend-Entwicklung >PHP-Tutorial >php多维数组根据键名快速查询其父键以及父键值

php多维数组根据键名快速查询其父键以及父键值

WBOY
WBOYOriginal
2016-07-25 09:04:271557Durchsuche
  1. /**
  2. * FILE_NAME : arr.php FILE_PATH : test/
  3. * 在多维数组中根据键名快速查询其父键以及父键值
  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' => '中国',
  18. 'cite' => array
  19. (
  20. 'beijing' => array
  21. (
  22. 'name' => '北京',
  23. 'site' => array('chaoyang' => '朝阳区', 'xuanwu' => '宣武区')
  24. ),
  25. 'shanghai' => array
  26. (
  27. 'name' => '上海',
  28. 'site' => array('jingan' => '静安区', 'huangpu' => '黄浦区')
  29. )
  30. )
  31. )
  32. );
  33. function printA($data)
  34. {
  35. echo '
    ';
  36. print_r($data);
  37. echo '';
  38. }
  39. function indexKey($data, $parent = NULL)
  40. {
  41. $arr = array();
  42. foreach ($data as $key => $value)
  43. {
  44. $arr[$key] = $parent;
  45. if (is_array($value))
  46. {
  47. $arr += indexKey($value, $key);
  48. }
  49. }
  50. return (Array)$arr;
  51. }
  52. printA(indexKey($arr));
  53. ?>
复制代码

打印出数据如下 Array ( [china] => [name] => china [cite] => china [beijing] => cite [site] => beijing [chaoyang] => site [xuanwu] => site [shanghai] => cite [jingan] => site [huangpu] => site ) 不过上面那样写存在一个问题,即:如果有同名键,会造成丢失,于是我写了这么一个类 只需要将数组传递给对象,对象提供两个接口 printArr 打印索引数组 search 查询键名的父数组键名 IndexKey创建查询索引查询类:

  1. /**
  2. * FILE_NAME : arr.php FILE_PATH : test/
  3. * 在多维数组中根据键名快速查询其父键以及父键值
  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' => '中国',
  18. 'cite' => array
  19. (
  20. 'beijing' => array
  21. (
  22. 'name' => '北京',
  23. 'site' => array('chaoyang' => '朝阳区', 'xuanwu' => '宣武区')
  24. ),
  25. 'shanghai' => array
  26. (
  27. 'name' => '上海',
  28. 'site' => array('jingan' => '静安区', 'huangpu' => '黄浦区')
  29. )
  30. )
  31. )
  32. );
  33. function printA($data)
  34. {
  35. echo '
    ';
  36. print_r($data);
  37. echo '';
  38. }
  39. function printP(IndexKey $obj, $key)
  40. {
  41. $parent = $obj->search($key);
  42. if ($parent)
  43. {
  44. echo '"'.$key.'" Parent Key is: ';
  45. if (!is_array($parent))
  46. {
  47. echo $parent."
    \n";
  48. }
  49. else printA($parent);
  50. }
  51. else echo 'NO Parent OR No Search of "'.$key.'"!'."

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

最后只差一个数据的输出了,于是我将这个类修改了下 提供了三个对外的方法 printArr 打印索引数组 search 查询键名的父数组键名 parentValue 查询父键值

  1. /**
  2. * FILE_NAME : arr.php FILE_PATH : test/
  3. * 在多维数组中根据键名快速查询其父键以及父键值
  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' => '中国',
  18. 'cite' => array
  19. (
  20. 'beijing' => array
  21. (
  22. 'name' => '北京',
  23. 'site' => array('chaoyang' => '朝阳区', 'xuanwu' => '宣武区')
  24. ),
  25. 'shanghai' => array
  26. (
  27. 'name' => '上海',
  28. 'site' => array('jingan' => '静安区', 'huangpu' => '黄浦区')
  29. )
  30. )
  31. )
  32. );
  33. function printA($data)
  34. {
  35. echo '
    ';
  36. print_r($data);
  37. echo '';
  38. }
  39. function printP2(IndexArr $obj, $key)
  40. {
  41. $parent = $obj->search($key);
  42. if (!is_array($parent))
  43. {
  44. if ($parent)
  45. {
  46. echo '"'.$key.'" Parent Key is: '.$parent."
    \n";
  47. }
  48. else echo 'NO Parent OR No Search of "'.$key.'"!'."
    \n";;
  49. echo '"'.$key.'" Parent "'.$parent.'" Value is: ';
  50. printA($obj->parentValue($key));
  51. }
  52. else printA($parent);
  53. }
  54. class IndexArr
  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]['parent'] : NULL;
  68. }
  69. public function parentValue($key)
  70. {
  71. return isset($this->_arr[$key]) ? $this->_arr[$key]['data'] : NULL;
  72. }
  73. private function _createIndex($data, $parent = NULL)
  74. {
  75. foreach ($data as $key => $value)
  76. {
  77. $this->_checkIndex($key, $parent, $data);
  78. if (is_array($value))
  79. {
  80. $this->_createIndex($value, $key);
  81. }
  82. }
  83. }
  84. private function _checkIndex($key, $parent, $data)
  85. {
  86. $data = $parent & isset($data[$parent]) ? $data[$parent] : $data;
  87. !isset($this->_arr[$key]) & $this->_arr[$key] = array('data' => $data, 'parent' => '');
  88. $index = &$this->_arr[$key]['parent'];
  89. if (!empty($index))
  90. {
  91. if (is_array($index))
  92. {
  93. array_push($index, $parent);
  94. }
  95. else $index = array($index, $parent);
  96. }
  97. else $index = $parent;
  98. }
  99. }
  100. $index2 = (Object)new IndexArr($arr);
  101. printA($index2->printArr());
  102. printP2($index2, 'beijing');
  103. printP2($index2, 'name');
  104. printP2($index2, 'china');
  105. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn