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

php中文网
发布: 2016-07-21 15:29:39
原创
1325人浏览过

我这么想的:
遍历一遍多维数组,将所有的键建立索引生成一个一维数组;
每次通过键名去查这个键的上级数组及数据
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 '
'; <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));
?>

打印出数据如下
Array
(
[china] =>
[name] => china
[cite] => china
[beijing] => cite
[site] => beijing
[chaoyang] => site
[xuanwu] => site
[shanghai] => cite
[jingan] => site
[huangpu] => site
)
不过上面那样写存在一个问题,即:如果有同名键,会造成丢失,于是我写了这么一个类
只需要将数组传递给对象,对象提供两个接口
printArr 打印索引数组
search 查询键名的父数组键名
IndexKey创建查询索引查询类:
复制代码 代码如下:

/**
* 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 '
'; <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');
?>

最后只差一个数据的输出了,于是我将这个类修改了下
提供了三个对外的方法
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 '
'; <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');
?>

源文件代码:php_arr.rar

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/323369.htmlTechArticle我这么想的: 遍历一遍多维数组,将所有的键建立索引生成一个一维数组; 每次通过键名去查这个键的上级数组及数据 OK,代码如下 inde...
相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号