php多維數組根據鍵名快速查詢其父鍵以及父鍵值

WBOY
發布: 2016-07-25 09:04:27
原創
1417 人瀏覽過
複製程式碼
  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' => '朝陽區', 'xuanwuwu ' => '宣武區')
  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. {
  63. public function printArr()
  64. {
  65. {
  66. return (Array)$this->_arr;
  67. }
  68. public function search($key)
  69. {
  70. return isset($this->_arr[$key]) ? $this->_arr [$key] : NULL;
  71. }
  72. private function _createIndex($data, $parent = NULL)
  73. {
  74. foreach ($data as $key => $value)
  75. {
  76. $this->_checkIndex($key, $parent);
  77. if (is_array($value))
  78. {
  79. $this->_createIndex($value, $key);
  80. }
  81. }
  82. }
  83. private function _checkIndex($key, $parent)
  84. {
  85. $index = isset($this->_arr[$key]) ? $this->_arr[ $key] : NULL;
  86. if ($index)
  87. {
  88. if (is_array($index))
  89. {
  90. array_push($this->_arr[$key], $parent );
  91. }
  92. else $this->_arr[$key] = array($index, $parent);
  93. }
  94. else $this->_arr[$key] = $parent;
  95. }
  96. }
  97. $index = (Object)new IndexKey($arr);
  98. printA($index->​​printArr());
printP($index, 'beijing') ;
printP($index, 'name');printP($index, 'china');?>
複製程式碼

最後只差一個資料的輸出了,於是我將這個類別修改了下 提供了三個對外的方法 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' => '朝陽區', 'xuanwuwu ' => '宣武區')
  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;_arr;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.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!