PHP code _php Tips for quickly querying its parent and parent key values in multidimensional arrays based on key names

Source: Internet
Author: User
That's what I think:
Iterate over the multidimensional array, all the keys to build an index to generate a one-dimensional array;
Each time through the key name to check the parent array of this key and data
OK, the code is as follows
Indexkey to create an indexed array function:
Copy Code code as follows:

<?php
/**
* FILE_NAME:arr.php file_path:test/
* Query the parent key and the parent key value quickly 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
(
' => ' array
(
' Name ' => ' China ',
' Cite ' => array
(
' Beijing ' => array
(
' Name ' => ' Beijing ',
' Site ' => array (' Chaoyang ' => ' Chaoyang District ', ' Xuanwu ' => ' Xuanwu District ')
),
' Shanghai ' => array
(
' Name ' => ' Shanghai ',
' Site ' => array (' Jingan ' => ' Jingan ', ' Huangpu ' => ' 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));
?>

Print out the following data
Array
(
[=>]
[Name] =>
[Cite] =>
[Beijing] => cite
[Site] => Beijing
[Chaoyang] => site
[Xuanwu] => site
[Shanghai] => cite
[Jingan] => site
[Huangpu] => site
)
But there is a problem with that: 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, which 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 Code code as follows:

<?php
/**
* FILE_NAME:arr.php file_path:test/
* Query the parent key and the parent key value quickly 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
(
' => ' array
(
' Name ' => ' China ',
' Cite ' => array
(
' Beijing ' => array
(
' Name ' => ' Beijing ',
' Site ' => array (' Chaoyang ' => ' Chaoyang District ', ' Xuanwu ' => ' Xuanwu District ')
),
' Shanghai ' => array
(
' Name ' => ' Shanghai ',
' Site ' => array (' Jingan ' => ' Jingan ', ' Huangpu ' => ' 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, ' it ');
?>

At the end of the output of only one data, so I modified the class
Provides three external methods
Printarr Print Index Array
The parent array key name of the search query key name
Parentvalue Query Parent Key value
Copy Code code as follows:

/**
* FILE_NAME:arr.php file_path:test/
* Query the parent key and the parent key value quickly 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
(
' => ' array
(
' Name ' => ' China ',
' Cite ' => array
(
' Beijing ' => array
(
' Name ' => ' Beijing ',
' Site ' => array (' Chaoyang ' => ' Chaoyang District ', ' Xuanwu ' => ' Xuanwu District ')
),
' Shanghai ' => array
(
' Name ' => ' Shanghai ',
' Site ' => array (' Jingan ' => ' Jingan ', ' Huangpu ' => ' 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. '" The 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;
}
}
$index 2 = (Object) new Indexarr ($arr);
PrintA ($index 2->printarr ());
PrintP2 ($index 2, ' Beijing ');
PrintP2 ($index 2, ' name ');
PrintP2 ($index 2, ' it ');
?>

Source file code: Php_arr.rar

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.