-
-
/**
- * FILE_NAME : arr.php FILE_PATH : test/
- * 在多維數組中根據鍵名快速查詢其父鍵以及父鍵值
- *
- * @copyright Copyright (c) 2006-2010
- * @author Levi
- * @package test.arr
- * @subpackage
- * @version 2011-04-29
- * @link bbs.it-home.org
- */
- header("Content-Type: text/html; charset=utf-8") ;
- $arr = array
- (
- 'china' => array
- (
- 'name' => '中國',
- 'cite' => array
- (
- 'beijing' => array
- (
- 'name' => '北京',
- 'site' => array('chaoyang' => '朝陽區', 'xuanwu' => '宣武區')
- ),
- 'shanghai' => array
- (
- 'name' => '上海',
- 'site' => array('jingan' => '靜安區', 'huangpu' => '黃浦區')
- )
- )
- )
- );
- function printA($data)
- {
- echo '
- print_r($data);
- echo '';
- }
- function indexKey($data, $parent = NULL)
- {
- $arr = array();
- foreach ($data as $key => $value)
- {
- $arr[$key] = $parent;
- if (is_array($value))
- {
- $arr += indexKey($value, $key);
- }
- }
- return (Array)$arr;
- }
- printA(indexKey($ arr));
- ?>
複製代碼
列印出資料如下
Array
(
[china] =>
[name] => china
[cite] => china
[beijing] => cite
[site] => beijing
[chaoyang] => site
[xuanwu] => site
[shanghai] => cite
[jingan] => site
[huangpu] => site
)
不過上面那樣寫存在一個問題,即:如果有同名鍵,會造成丟失,於是我寫了這麼一個類
只需要將數組傳遞給對象,對象提供兩個接口
printArr 列印索引數組
search 查詢鍵名的父數組鍵名
IndexKey建立查詢索引查詢類別:
-
-
/**
- * FILE_NAME : arr.php FILE_PATH : test/
- * 在多維數組中根據鍵名快速查詢其父鍵以及父鍵值
- *
- * @copyright Copyright (c) 2006-2010
- * @author Levi
- * @package test.arr
- * @subpackage
- * @version 2011-04-29
- * @link bbs.it-home.org
- */
- header("Content-Type: text/html; charset=utf- 8");
- $arr = array
- (
- 'china' => array
- (
- 'name' => '中國',
- 'cite' => array
- (
- 'beijing' => array
- (
- 'name' => '北京',
- 'site' => array('chaoyang' => '朝陽區', 'xuanwuwu ' => '宣武區')
- ),
- 'shanghai' => array
- (
- 'name' => '上海',
- 'site' => array('jingan ' => '靜安區', 'huangpu' => '黃浦區')
- )
- )
- )
- );
- function printA($data)
- {
- echo '
- print_r($data);
- echo '';
- }
- function printP(IndexKey $obj, $key)
- {
- $parent = $obj->search($key);
- if ($parent)
- {
- echo '"'.$key.'" Parent Key is: ';
- if (!is_array($parent))
- {
- echo $parent."
n";
- }
- else printA($parent);
- }
- else echo 'NO Parent OR No Search of "'.$key.'"!'."
n";
- }
- class IndexKey
- {
- private $_arr = array();
- public function __construct($data)
- {
- $this->_createIndex($data);
- }
- public function printArr()
- {
- {
- public function printArr()
- {
- {
- return (Array)$this->_arr;
- }
- public function search($key)
- {
- return isset($this->_arr[$key]) ? $this->_arr [$key] : NULL;
- }
- private function _createIndex($data, $parent = NULL)
- {
- foreach ($data as $key => $value)
- {
- $this->_checkIndex($key, $parent);
- if (is_array($value))
- {
- $this->_createIndex($value, $key);
- }
- }
- }
- private function _checkIndex($key, $parent)
- {
- $index = isset($this->_arr[$key]) ? $this->_arr[ $key] : NULL;
- if ($index)
- {
- if (is_array($index))
- {
- array_push($this->_arr[$key], $parent );
- }
- else $this->_arr[$key] = array($index, $parent);
- }
- else $this->_arr[$key] = $parent;
- }
- }
- $index = (Object)new IndexKey($arr);
- printA($index->printArr());
printP($index, 'beijing') ; printP($index, 'name'); printP($index, 'china');?> 複製程式碼最後只差一個資料的輸出了,於是我將這個類別修改了下
提供了三個對外的方法
printArr 列印索引數組
search 查詢鍵名的父數組鍵名
parentValue 查詢父鍵值
-
-
/**
- * FILE_NAME : arr.php FILE_PATH : test/
- * 在多維數組中根據鍵名快速查詢其父鍵以及父鍵值
- *
- * @copyright Copyright (c) 2006-2010
- * @author Levi
- * @package test.arr
- * @subpackage
- * @version 2011-04-29
- * @link bbs.it-home.org
- */
- header("Content-Type: text/html; charset=utf- 8");
- $arr = array
- (
- 'china' => array
- (
- 'name' => '中國',
- 'cite' => array
- (
- 'beijing' => array
- (
- 'name' => '北京',
- 'site' => array('chaoyang' => '朝陽區', 'xuanwuwu ' => '宣武區')
- ),
- 'shanghai' => array
- (
- 'name' => '上海',
- 'site' => array('jingan ' => '靜安區', 'huangpu' => '黃浦區')
- )
- )
- )
- );
- function printA($data)
- {
- echo '
- print_r($data);
- echo '';
- }
- function printP2(IndexArr $obj, $key)
- {
- $parent = $obj->search($key);
- if (!is_array($parent))
- {
- if ($parent)
- {
- echo '"' .$key.'" Parent Key is: '.$parent."
n";
- }
- else echo 'NO Parent OR No Search of "'.$key.'"!'. "
n";;
- echo '"'.$key.'" Parent "'.$parent.'" Value is: ';
- printA($obj->parentValue($key) );
- }
- else printA($parent);
- }
- class IndexArr
- {
- private $_arr = array();
- public function __construct($data)
- {
- $this->_createIndex($data);
- }
- public function printArr()
- {
- return (Array)$this->_arr;_arr;
public function search($key)- {
- return isset($this->_arr[$key]) ? $this->_arr[$key]['parent'] : NULL;
- }
- public function parentValue($key)
- {
- return isset($this->_arr[$key]) ? $this->_arr[$key]['data'] : NULL;
- }
- private function _createIndex($data, $parent = NULL)
- {
- foreach ($data as $key => $value)
- {
- $this->_checkIndex ($key, $parent, $data);
- if (is_array($value))
- {
- $this->_createIndex($value, $key);
- }
- }
- }
- private function _checkIndex($key, $parent, $data)
- {
- $data = $parent & isset($data[$parent]) ? $data[$parent] : $data;
- !isset($this->_arr[$key]) & $this->_arr[$key] = array('data' => $data, 'parent' => '');
- $index = &$this->_arr[$key]['parent'];
- if (!empty($index))
- {
- if (is_array($index))
- {
- array_push($index, $parent);
- }
- else $index = array($index, $parent);
- }
- else $index = $parent;
- }
- }
- $index2 = (Object)new IndexArr($arr);
- printA($index2->printArr());
- printP2($index2, 'beijing');
- printP2($index2, 'name');
- printP2($index2, 'china');
- ?>
複製程式碼
| 複製程式碼