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

php中文网
发布: 2016-07-25 09:04:27
原创
1599人浏览过
  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 '
    ';<li>print_r($data);<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. ?>
复制代码

打印出数据如下 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 '
    ';<li>print_r($data);<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. ?>
复制代码

最后只差一个数据的输出了,于是我将这个类修改了下 提供了三个对外的方法 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 '
    ';<li>print_r($data);<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."
    \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. ?>
复制代码


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号