Php quickly queries the code of its parent key and parent key value based on the key name in a multi-dimensional array.

Source: Internet
Author: User
Tags createindex
There is a multi-dimensional array, which can be customized. Suppose we want to find a key in this array for me:
Traverse multi-dimensional arrays and create an index for all keys to generate a one-dimensional array;
Check the upper-level array and data of the key by using the key name each time.
OK. the code is as follows:
IndexKey:
The code is as follows:
/**
* 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 mail: levi@cgfeel.com
* @ Author Levi
* @ Package test. arr
* @ Subpackage
* @ Version 2011-04-29
*/
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 data as follows:
Array
(
[China] =>
[Name] => china
[Cite] => china
[Beijing] => cite
[Site] => beijing
[Chaoyang] => site
[Xuanwu] => site
[Shanghai] => cite
[Jingan] => site
[Huangpu] => site
)
However, there is a problem with writing like this: if there is a key with the same name, it will lead to loss, so I wrote such a class
You only need to pass the array to the object. the object provides two interfaces.
PrintArr print index array
Parent array key name of the search query key name
IndexKey:
The code is as follows:
/**
* 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 mail: levi@cgfeel.com
* @ Author Levi
* @ Package test. arr
* @ Subpackage
* @ Version 2011-04-29
*/
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.
Three external methods are provided.
PrintArr print index array
Parent array key name of the search query key name
ParentValue query parent key value
The code is as follows:
/**
* 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 mail: levi@cgfeel.com
* @ Author Levi
* @ Package test. arr
* @ Subpackage
* @ Version 2011-04-29
*/
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 ');
?>

Source File Code: php_arr.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.