PHP的:获得独立阵列钥匙 [英] PHP: get keys of independent arrays
问题描述
我有两个数组。
第一阵列的示例:
$ arrayOne =阵列
(
'果'=>阵列(
'苹果'=>阵列()
'桔子'=>阵列()
'香蕉'=>阵列()
)
'蔬菜'=>阵列(
'西红柿'=>阵列()
'胡萝卜'=>阵列()
'芹菜'=>阵列()
'甜菜'=>阵列
(
'熊'=>阵列()
太空堡垒 - 卡拉狄加'=>阵列()
)
)
'肉'=>阵列()
'其他'=>阵列()
);
2日:
$ arrayTwo =阵列
(
frewt'=>阵列(
aplz'=>阵列()
orangeez'=>阵列()
bunanahs'=>阵列()
)
vetchteblz'=>阵列(
toem8ohs'=>阵列()
careodds'=>阵列()
卖出-R-E'=>阵列()
'节拍'=>阵列
(
裸Z'=>阵列()
tablestar-neglectia'=>阵列()
)
)
'整齐'=>阵列()
'妈妈'=>阵列()
);
注意,两个数组中的精确相同的格式(相同数量的尺寸,按键数目,顺序,等,等),只有键的名称不同。 (数组键基本保存所有的数据。)
我有解决第一个数组的键( $ arrayOne
)的几个变量。例如。 $有一个
将解决第一个数组的第一个维度,所以它的值(字符串)将是一出'果'$ C $的C>,
'蔬菜'
,'肉'
或其他
。结果 $两个
将'苹果'
或'橘子'
或香蕉
或'西红柿'
或'胡萝卜'
等等,你的想法。 (有瓦尔每个维度)结果
正如我所说,这些变量只能解决 $ arrayOne
。我希望能够解决第二阵列过于中的键,虽然。这意味着,通过观察值 $有一个
我希望能够得到两个数组的array_key。
$ arrayOne = // ...$ arrayTwo = // ...功能为getPosition(数组$改编,$键){
$它=新RecursiveIteratorIterator(新RecursiveArrayIterator($ ARR)
RecursiveIteratorIterator :: SELF_FIRST);
$ POS =阵列();
的foreach($它作为$ K => $ V){
如果(计数($ POS) - 1> $ IT-> getDepth()){
array_pop($ POS)
$ POS [$它 - > getDepth()] ++;
}
ELSEIF(计数($ POS) - 1< $ IT-> getDepth()){
array_push($ POS,0);
}
其他{
$ POS [$它 - > getDepth()] ++;
}
如果($ķ=== $键){
返回$ POS;
}
}
}功能getElementKey(数组$改编,数组$位置){
$ CUR = $编曲;
$ curkey = NULL;
的foreach(如$ P $位置){
重置($ CUR);
为($ I = 0; $ I< $ P; $ I ++){
下一个($ CUR);
}
$ curkey =键($ CUR);
$ CUR =电流($ CUR);
}
返回$ curkey;
}后续代码var_dump(为getPosition($ arrayOne,太空堡垒 - 卡拉狄加));
的var_dump(getElementKey($ arrayTwo,阵列(1,3,1)));
给出了:
阵列(3){
[0] =>
INT(1)
[1] =>
INT(3)
[2] =>
INT(1)
}
串(19)tablestar-neglectia
您可以为getPosition
的结果输送到 getElementKey
:
getElementKey($ arrayTwo为getPosition($ arrayOne,太空堡垒 - 卡拉狄加));
I have two arrays.
Example of the first array:
$arrayOne = array ( 'fruit' => array( 'apples' => array(), 'oranges' => array(), 'bananas' => array() ), 'vegetables' => array( 'tomatoes' => array(), 'carrots' => array(), 'celery' => array(), 'beets' => array ( 'bears' => array(), 'battlestar-galactica' => array() ), ), 'meat' => array(), 'other' => array() );
2nd:
$arrayTwo = array ( 'frewt' => array( 'aplz' => array(), 'orangeez' => array(), 'bunanahs' => array() ), 'vetchteblz' => array( 'toem8ohs' => array(), 'careodds' => array(), 'sell-R-e' => array(), 'beats' => array ( 'bare z' => array(), 'tablestar-neglectia' => array() ), ), 'neat' => array(), 'mother' => array() );
Notice that the two arrays are in the exact same "format" (same number of dimensions, number of keys, order, etc., etc.), only the names of the keys differ. (The array keys basically hold all the data.)
I have a few variables that address the keys of the first array ($arrayOne
). E.g. $one
would address the first dimension of the first array, so it's value (string) would be one out of 'fruit'
, 'vegetables'
, 'meat'
or 'other'
.
$two
would be 'apples'
or 'oranges'
or 'bananas'
or 'tomatoes'
or 'carrots'
, etc., you get the idea. (There's vars for each dimension)
As I said, those variables only address $arrayOne
. I want to be able to address the keys in the second array too, though. Meaning, by looking at the value of $one
I want to be able to get the array_key of both arrays.
$arrayOne = //...
$arrayTwo = //...
function getPosition(array $arr, $key) {
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr),
RecursiveIteratorIterator::SELF_FIRST);
$pos = array();
foreach ($it as $k => $v) {
if (count($pos) - 1 > $it->getDepth()) {
array_pop($pos);
$pos[$it->getDepth()]++;
}
elseif (count($pos) - 1 < $it->getDepth()) {
array_push($pos, 0);
}
else {
$pos[$it->getDepth()]++;
}
if ($k === $key) {
return $pos;
}
}
}
function getElementKey(array $arr, array $position) {
$cur = $arr;
$curkey = null;
foreach ($position as $p) {
reset($cur);
for ($i = 0; $i < $p; $i++) {
next($cur);
}
$curkey = key($cur);
$cur = current($cur);
}
return $curkey;
}
var_dump(getPosition($arrayOne, "battlestar-galactica"));
var_dump(getElementKey($arrayTwo, array(1, 3, 1)));
gives:
array(3) { [0]=> int(1) [1]=> int(3) [2]=> int(1) } string(19) "tablestar-neglectia"
You can feed the result of getPosition
to getElementKey
:
getElementKey($arrayTwo, getPosition($arrayOne, "battlestar-galactica"));
这篇关于PHP的:获得独立阵列钥匙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!