- /**
- * FILE_NAME: arr. php FILE_PATH: test/
- * Quick query of the parent key and parent key value based on the key name in a multi-dimensional array
- *
- * @ 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' => 'China ',
- 'Cite '=> array
- (
- 'Beijing' => array
- (
- 'Name' => 'Beijing ',
- 'Site' => array ('chunyang' => 'chaoyang District ', 'xuanwu' => 'xuanwu District ')
- ),
- 'Shanghai' => array
- (
- 'Name' => 'Shanghai ',
- 'Site' => array ('jinganc' => 'jing 'an District ', 'huangpu' => 'huangpu District ')
- )
- )
- )
- );
- 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 ));
- ?>
Print the following data: Array ([china] => [name] => china [cite] => china [beijing] => cite [site] => beijing [chaoyang] => site [xuanwu] => site [shanghai] => cite [jingan] => site [huangpu] => site) but there is a problem with writing like this: if there is a key with the same name, it will lead to loss, so I write such a class and only need to pass the array to the object, the object provides two interfaces: printArr printing index array search query key name parent array key name IndexKey creation query index query class:
- /**
- * FILE_NAME: arr. php FILE_PATH: test/
- * Quick query of the parent key and parent key value based on the key name in a multi-dimensional array
- *
- * @ 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' => 'China ',
- 'Cite '=> array
- (
- 'Beijing' => array
- (
- 'Name' => 'Beijing ',
- 'Site' => array ('chunyang' => 'chaoyang District ', 'xuanwu' => 'xuanwu District ')
- ),
- 'Shanghai' => array
- (
- 'Name' => 'Shanghai ',
- 'Site' => array ('jinganc' => 'jing 'an District ', 'huangpu' => 'huangpu District ')
- )
- )
- )
- );
- 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 ()
- {
- 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 ');
- ?>
At last, only one data output is missing, so I modified this class and provided three external methods: printArr print index array search query key name parent array key name parentValue query parent key value
- /**
- * FILE_NAME: arr. php FILE_PATH: test/
- * Quick query of the parent key and parent key value based on the key name in a multi-dimensional array
- *
- * @ 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' => 'China ',
- 'Cite '=> array
- (
- 'Beijing' => array
- (
- 'Name' => 'Beijing ',
- 'Site' => array ('chunyang' => 'chaoyang District ', 'xuanwu' => 'xuanwu District ')
- ),
- 'Shanghai' => array
- (
- 'Name' => 'Shanghai ',
- 'Site' => array ('jinganc' => 'jing 'an District ', 'huangpu' => 'huangpu District ')
- )
- )
- )
- );
- 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;
- }
- 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 ');
- ?>
|