当前位置:Gxlcms > PHP教程 > php多维数组根据键名快速查询其父键以及父键值

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

时间:2021-07-01 10:21:17 帮助过:28人阅读

  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. }
  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. ?>

打印出数据如下 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. }
  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. ?>

最后只差一个数据的输出了,于是我将这个类修改了下 提供了三个对外的方法 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. }
  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. ?>

人气教程排行