php在多维数组中根据键名快速查询其父键以及父键值的代码

2015-01-24信息快讯网

有一个多维数组,有多少维大家可以自定义。假如我们要在这个数组中找一个键为'subIndex'的值,我们可以用for、foreach等方法遍历查找 反过来,假如我们任意给出一个或多个键,要求找出这个键的父级数组的键和值。这又如何实现?

我这么想的:
遍历一遍多维数组,将所有的键建立索引生成一个一维数组;
每次通过键名去查这个键的上级数组及数据
OK,代码如下
indexKey创建索引数组函数:
 
<?php 
/** 
* FILE_NAME : arr.php FILE_PATH : test/ 
* 在多维数组中根据键名快速查询其父键以及父键值 
* 
* @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' => '中国', 
'cite' => array 
( 
'beijing' => array 
( 
'name' => '北京', 
'site' => array('chaoyang' => '朝阳区', 'xuanwu' => '宣武区') 
), 
'shanghai' => array 
( 
'name' => '上海', 
'site' => array('jingan' => '静安区', 'huangpu' => '黄浦区') 
) 
) 
) 
); 
function printA($data) 
{ 
echo '<pre>'; 
print_r($data); 
echo '</pre>'; 
} 
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)); 
?> 

打印出数据如下
Array
(
[china] =>
[name] => china
[cite] => china
[beijing] => cite
[site] => beijing
[chaoyang] => site
[xuanwu] => site
[shanghai] => cite
[jingan] => site
[huangpu] => site
)
不过上面那样写存在一个问题,即:如果有同名键,会造成丢失,于是我写了这么一个类
只需要将数组传递给对象,对象提供两个接口
printArr 打印索引数组
search 查询键名的父数组键名
IndexKey创建查询索引查询类:
 
<?php 
/** 
* FILE_NAME : arr.php FILE_PATH : test/ 
* 在多维数组中根据键名快速查询其父键以及父键值 
* 
* @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' => '中国', 
'cite' => array 
( 
'beijing' => array 
( 
'name' => '北京', 
'site' => array('chaoyang' => '朝阳区', 'xuanwu' => '宣武区') 
), 
'shanghai' => array 
( 
'name' => '上海', 
'site' => array('jingan' => '静安区', 'huangpu' => '黄浦区') 
) 
) 
) 
); 
function printA($data) 
{ 
echo '<pre>'; 
print_r($data); 
echo '</pre>'; 
} 
function printP(IndexKey $obj, $key) 
{ 
$parent = $obj->search($key); 
if ($parent) 
{ 
echo '"'.$key.'" Parent Key is: '; 
if (!is_array($parent)) 
{ 
echo $parent."<br />\n"; 
} 
else printA($parent); 
} 
else echo 'NO Parent OR No Search of "'.$key.'"!'."<br /><br />\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'); 
?> 

最后只差一个数据的输出了,于是我将这个类修改了下
提供了三个对外的方法
printArr 打印索引数组
search 查询键名的父数组键名
parentValue 查询父键值
 
/** 
* FILE_NAME : arr.php FILE_PATH : test/ 
* 在多维数组中根据键名快速查询其父键以及父键值 
* 
* @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' => '中国', 
'cite' => array 
( 
'beijing' => array 
( 
'name' => '北京', 
'site' => array('chaoyang' => '朝阳区', 'xuanwu' => '宣武区') 
), 
'shanghai' => array 
( 
'name' => '上海', 
'site' => array('jingan' => '静安区', 'huangpu' => '黄浦区') 
) 
) 
) 
); 
function printA($data) 
{ 
echo '<pre>'; 
print_r($data); 
echo '</pre>'; 
} 
function printP2(IndexArr $obj, $key) 
{ 
$parent = $obj->search($key); 
if (!is_array($parent)) 
{ 
if ($parent) 
{ 
echo '"'.$key.'" Parent Key is: '.$parent."<br />\n"; 
} 
else echo 'NO Parent OR No Search of "'.$key.'"!'."<br />\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'); 
?> 

源文件代码:php_arr.rar
PHP 之Section与Cookie使用总结
PHP 文件系统详解
PHP 面向对象详解
关于访问控制的一首PHP面试题(对属性或方法的访问控制)
PHP基础陷阱题(变量赋值)
php比较多维数组中值的大小排序实现代码
PHP输出数组中重名的元素的几种处理方法
PHP手机号码归属地查询代码(API接口/mysql)
php数组一对一替换实现代码
PHP实现手机归属地查询API接口实现代码
php中数组首字符过滤功能代码
PHP常用开发函数解析之数组篇[未完结]
最新用php获取谷歌PR值算法,附上php查询PR值代码示例
php数组函数序列之array_flip() 将数组键名与值对调
php数组函数序列之array_search()- 按元素值返回键名
php数组函数序列之krsort()- 对数组的元素键名进行降序排序,保持索引关系
php数组函数序列之ksort()对数组的元素键名进行升序排序,保持索引关系
php数组函数序列之each() - 获取数组当前内部指针所指向元素的键名和键值,并将指针移到下一位
PHP版国家代码、缩写查询函数代码
pdo中使用参数化查询sql
PHP新手入门学习方法
php 大数据量及海量数据处理算法总结
php 截取字符串并以零补齐str_pad() 函数
php调用mysql数据 dbclass类
用PHP的超级变量$_POST获取HTML表单(HTML Form) 数据
用PHP的超级变量$_GET获取HTML表单(Form) 数据
php中处理mysql_fetch_assoc返回来的数组 不用foreach----echo
PHP遍历二维数组的代码
php中用数组的方法设置cookies
php中显示数组与对象的实现代码
PHP学习之数组值的操作
PHP开发中四种查询返回结果分析
为IP查询添加GOOGLE地图功能的代码
PHP实现多条件查询实例代码
PHP中开发XML应用程序之基础篇 添加节点 删除节点 查询节点 查询节
php 数组操作(增加,删除,查询,排序)等函数说明第1/2页
PHP去除数组中重复的元素并按键名排序函数
©2014-2024 dbsqp.com