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. If we want to find a subIndex value in this array, we can use methods such as for and foreach to traverse and find the reverse. If we give one or more keys at will, you must find the keys and values of the parent-level array of the key. How can this be achieved?

There is a multi-dimensional array, which can be customized. If we want to find a value with the key 'subindex 'In this array, we can use methods such as for and foreach to traverse and find the reverse. If we give one or more keys at will, you must find the keys and values of the parent-level array of the key. How can this be achieved?

I think so:
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:

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.