路径数组值深层多维数组 [英] path to array value deep multidimensional array
问题描述
我有一个数组,我需要给定值的路径(键)。我写了一个函数,但我无法得到它的工作,该阵列可以有无限的深度另一个功能被控制,我还没有决定限制,但尚未深度是可变的,它的实际工作起来到一个点,但深度可能是30-40-50深,所以我需要它的工作的方式,这个功能实际上只是价值认同,数量也模板,标签是模板标签,每个标签都有一个单一的与之相关联的模板,模板的所有名称是独一无二的标签名称是不是因为他们只用一个模板关联,ID值由破折号分开,其中模板ID只是数字和标签(模板和标签ID非法的) IDS都包裹着squigglies和像这样{$} TAG,不必担心重复的值,因为无限循环被禁止(模板不能链接到自己或链接到自己的模板),加上IDS给出一个美元符号只有模板,其中模板名称是唯一的。给定数组中命名为4模板的ID是0 - {$ CONTENT} -2 - {$} PARAGRAPH -4,在那里我失去它是我的功能不会超过这个深度,感谢您的帮助
阵列(大小= 1)
0 =>
阵列(大小= 4)
'{$ TITLE}'=>空值
'{$ NAME}'=>空值
'{$ FRIENDS}'=>
阵列(大小= 1)
1 =>
阵列(尺寸= 2)的
'{$}朋友'=>空值
'{$朋友[$ i]} =>空值
'{$ CONTENT}'=>
阵列(大小= 1)
2 =>
'{$ HEADING}'=>
阵列(大小= 1)
3 =>
阵列(大小= 0)
空
'{$ PARAGRAPH}'=>
阵列(大小= 1)
4 =>
阵列(大小= 1)
'{$ AnotherParagraph}'=>空值
这里是我的功能,我已经添加了深度变量只是为了测试目的,$这个 - >家庭是上面给出的数组,调用该函数:$ n = $这个 - > get_id(4);
公共职能get_id($成员,家庭$ = NULL,$ ID = NULL,$深度= 0){
如果(空($这个 - >家庭)){
返回false;
}
如果(is_null($家族)){
$家庭= $这个 - >家庭;
} 的foreach($家庭为$父=> $ tag_child){
如果($成员=== $父){
返回$成员;
} 的foreach($ tag_child为$标签=> $子){
如果(is_null($子)||空($子)){
继续;
} $ childkey =键($子女); 如果($成员=== $ childkey){
$ ID =' - '$父。' - $成员。;'$标记。' - '。
$ ID = LTRIM($ ID,' - ');
返回的$ id;
} $家庭= $子女; 如果(is_null($编号)及!&放大器;!空($编号)){
$ id_array =爆炸(' - ',$ id)的;
的foreach($ id_array为$ ID_VALUE){
如果($ ID_VALUE!== $ childkey){
$ new_id_array [] = $ ID_VALUE;
}其他{
打破;
}
}
$ ID =破灭(' - ',$ new_id_array);
} 如果($父=== 0){
的$ id = $父。' - '$标签。' - '$ childkey。
}其他{
$ ID =' - '$标签。' - '$ childkey。;
} $ ID = LTRIM($ ID,' - '); $深度++; 的$ id = $这个 - > get_id($成员,家庭$,$ ID,$深度); } }
}
使用递归函数。这里有一个我写了这个确切目的。
公共职能recurseArray($数组$ builtKey =){
$值=阵列();
的foreach($数组$关键=> $值){
如果(is_array($值)){
如果(!空($ builtKey)){
$值= array_merge($值,recurseArray(价值$,$ builtKey $键)。。);
}其他{
$值= array_merge($值,recurseArray(价值$,$键));
}
}其他{
如果(!空($ builtKey)){
$值[$ builtKey $键。] = $价值;
}其他{
$值[$关键] = $价值;
}
}
}
返回$价值;
}
这会导致一个平面阵列转换这样的:
阵列(
钥匙=>阵列(
的=>阵列(
多=>阵列(
深度=> 值
)
)
)
KEY2=> 值
);
进入这个:
阵列(
key.of.many.depths=> 值,
KEY2=> 值
);
I have an array where I need the path (keys) to a given value. I've written a function for it but I can't get it working, the array can have an infinite depth to be controlled in another function, I haven't decided on a limit yet but the depth is variable, it actually works up to a point but the depth may be 30-40-50 deep so I need it to work that way, this function is really just for value identification, the numbers are templates, the tags are tags in the templates, each tag has a single template associated with it, all template names are unique and tags names aren't because they are only associated with a single template, id values are separated by a dash (illegal in template and tag ids), where template ids are only numeric and tag ids are wrapped with squigglies and a dollar sign like so {$TAG}, don't have to worry about duplicate values because infinite loops are forbidden (template can't link to itself or a template that links to itself) plus ids are given only to templates where template names are unique. The id of the template named 4 in the given array would be 0-{$CONTENT}-2-{$PARAGRAPH}-4, where I lose it is my function won't go beyond this depth, thanks for the help
array (size=1)
0 =>
array (size=4)
'{$TITLE}' => null
'{$NAME}' => null
'{$FRIENDS}' =>
array (size=1)
1 =>
array (size=2)
'{$friend}' => null
'{$friends[$i]}' => null
'{$CONTENT}' =>
array (size=1)
2 =>
'{$HEADING}' =>
array (size=1)
3 =>
array (size=0)
empty
'{$PARAGRAPH}' =>
array (size=1)
4 =>
array (size=1)
'{$AnotherParagraph}' => null
here is my function, I've added the depth variable just for testing purposes, $this->family is the array given above, calling the function: $id = $this->get_id(4);
public function get_id($member, $family=null, $id=null, $depth=0) {
if (empty($this->family)) {
return false;
}
if (is_null($family)) {
$family = $this->family;
}
foreach ($family as $parent => $tag_child) {
if ($member === $parent) {
return $member;
}
foreach ($tag_child as $tag => $child) {
if (is_null($child) || empty($child)) {
continue;
}
$childkey = key($child);
if ($member === $childkey) {
$id .= '-'.$parent.'-'.$tag.'-'.$member;
$id = ltrim($id, '-');
return $id;
}
$family = $child;
if (!is_null($id) && !empty($id)) {
$id_array = explode('-', $id);
foreach ($id_array as $id_value) {
if ($id_value !== $childkey) {
$new_id_array[] = $id_value;
}else{
break;
}
}
$id = implode('-', $new_id_array);
}
if ($parent === 0) {
$id = $parent.'-'.$tag.'-'.$childkey;
}else{
$id .= '-'.$tag.'-'.$childkey;
}
$id = ltrim($id, '-');
$depth++;
$id = $this->get_id($member, $family, $id, $depth);
}
}
}
Use a recursive function. Here's one I wrote for this EXACT purpose.
public function recurseArray($array, $builtKey = "") {
$values = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
if (!empty($builtKey)) {
$values = array_merge($values, recurseArray($value, $builtKey.".".$key));
} else {
$values = array_merge($values, recurseArray($value, $key));
}
} else {
if (!empty($builtKey)) {
$values[$builtKey.".".$key] = $value;
} else {
$values[$key] = $value;
}
}
}
return $values;
}
This results in a flat array that transforms this:
array(
"key" => array(
"of" => array(
"many" => array(
"depths" => "value"
)
)
),
"key2" => "value"
);
Into this:
array(
"key.of.many.depths" => "value",
"key2" => "value"
);
这篇关于路径数组值深层多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!