Home  >  Article  >  Backend Development  >  PHP code to quickly query the parent key and parent key value based on the key name in a multi-dimensional array_PHP tutorial

PHP code to quickly query the parent key and parent key value based on the key name in a multi-dimensional array_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:29:391017browse

What I think:
Traverse the multi-dimensional array once, index all the keys to generate a one-dimensional array;
Check the upper-level array and data of this key through the key name each time
OK, the code is as follows
indexKey creates index array function:

Copy code The code is as follows:

/**
* FILE_NAME : arr.php FILE_PATH : test/
* Quickly query the parent key and parent key value according to 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('chaoyang' => 'Chaoyang District', 'xuanwu' => 'Xuanwu District')
),
'shanghai' => array
(
'name' => 'Shanghai',
'site' => array('jingan' => 'Jing'an District', 'huangpu' => ' Huangpu District')
)
)
)
);
function printA($data)
{
echo '
'; <br>print_r( $data); <br>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] => ; cite
[jingan] => site
[huangpu] => site
)
However, there is a problem with writing the above, that is: if there is a key with the same name, it will be lost, so I After writing such a class
, you only need to pass the array to the object. The object provides two interfaces
printArr to print the index array
search to query the parent array key name of the key name
IndexKey to create a query index query class:



Copy code
The code is as follows: /**
* FILE_NAME : arr.php FILE_PATH : test/
* Quickly query the parent key and parent key value according to 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('chaoyang' => 'Chaoyang District', 'xuanwu' => 'Xuanwu District')
),
'shanghai' => array
(
'name' => 'Shanghai',
'site' => array('jingan' => 'Jing'an District', 'huangpu' => 'Huangpu District')
)
)
)
);
function printA($data)
{
echo '
'; <br>print_r($data); <br>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');
?>


In the end, there was only one data output left, so I modified this class
Provides three external methods
printArr prints the index array
search queries the parent array key name of the key name
parentValue Query the parent key value
Copy code The code is as follows:

/**
* FILE_NAME : arr.php FILE_PATH : test/
* Quickly query the parent key and parent key value according to 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('chaoyang' => 'Chaoyang District', 'xuanwu' => 'Xuanwu District')
),
'shanghai' => array
(
'name' => 'Shanghai',
'site' => array('jingan' => 'Jing'an District', 'huangpu' => 'Huangpu District')
)
)
)
);
function printA($data)
{
echo '
'; <br>print_r($data); <br>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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323369.htmlTechArticleWhat I think: Traverse the multi-dimensional array, index all the keys to generate a one-dimensional array; each time OK to check the superior array and data of this key through the key name, the code is as follows inde...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn