I think so:
Iterate through multidimensional arrays and create a one-dimensional array by indexing all the keys;
Each time through the key name to check the parent array and data
OK, the code is as follows
Indexkey Creating an indexed array function:
Copy CodeThe code is as follows:
/**
* FILE_NAME:arr.php file_path:test/
* Quickly query the parent key and the parent key value in a multidimensional array based on the key name
*
* @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 (' Chaoyang ' = ' Chaoyang ', ' Xuanwu ' + ' Xuanwu ')
),
' Shanghai ' = array
(
' Name ' = ' Shanghai ',
' site ' = = Array (' Jingan ' = ' Jingan ', ' huangpu ' = ' Huangpu ')
)
)
)
);
function PrintA ($data)
{
Echo '
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 out 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 the above, that is, if there is a key with the same name, it will cause loss, so I wrote a class
Only need to pass the array to the object, the object provides two interfaces
Printarr Print Index Array
The parent array key name of the search query key name
Indexkey Create query Index query class:
Copy CodeThe code is as follows:
/**
* FILE_NAME:arr.php file_path:test/
* Quickly query the parent key and the parent key value in a multidimensional array based on the key name
*
* @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 (' Chaoyang ' = ' Chaoyang ', ' Xuanwu ' + ' Xuanwu ')
),
' Shanghai ' = array
(
' Name ' = ' Shanghai ',
' site ' = = Array (' Jingan ' = ' Jingan ', ' huangpu ' = ' Huangpu ')
)
)
)
);
function PrintA ($data)
{
Echo '
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 ');
?>
The last one is the output of a data, so I changed this class to the next
Provides three external methods
Printarr Print Index Array
The parent array key name of the search query key name
Parentvalue Querying Parent Key values
Copy CodeThe code is as follows:
/**
* FILE_NAME:arr.php file_path:test/
* Quickly query the parent key and the parent key value in a multidimensional array based on the key name
*
* @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 (' Chaoyang ' = ' Chaoyang ', ' Xuanwu ' + ' Xuanwu ')
),
' Shanghai ' = array
(
' Name ' = ' Shanghai ',
' site ' = = Array (' Jingan ' = ' Jingan ', ' huangpu ' = ' Huangpu ')
)
)
)
);
function PrintA ($data)
{
Echo '
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;
}
}
$index 2 = (Object) new Indexarr ($arr);
PrintA ($index 2->printarr ());
PrintP2 ($index 2, ' Beijing ');
PrintP2 ($index 2, ' name ');
PrintP2 ($index 2, ' China ');
?>
Source file code: Php_arr.rar
http://www.bkjia.com/PHPjc/323369.html www.bkjia.com true http://www.bkjia.com/PHPjc/323369.html techarticle I think so: iterate through multidimensional arrays, make all the keys indexed to generate a one-dimensional array, each time through the key name to check the parent array of the key and data OK, the code is as follows Inde ...