Php 如何从键数组中获取数组的最后一个元素?
要求: 步骤1:转到Php 如何从键数组中获取数组的最后一个元素?,php,multidimensional-array,associative-array,Php,Multidimensional Array,Associative Array,要求: 步骤1:转到$parent最后一个元素(即$parent[2] 步骤2:从$parent[1]检查所有具有数组值的键。此处b键具有数组值。转到b键last元素(即b键[3] 步骤3:从b键[3]检查所有具有数组值的键。此处16具有数组值。在此处附加新数据 预期输出:“16”=>数组(数组(“新数据”))演示我的评论: $parent = array( array(//$parent[0] "a-key",
$parent
最后一个元素(即$parent[2]
步骤2:从$parent[1]
检查所有具有数组值的键。此处b键
具有数组值。转到b键
last元素(即b键[3]
步骤3:从b键[3]
检查所有具有数组值的键。此处16
具有数组值。在此处附加新数据
预期输出:
“16”=>数组(数组(“新数据”))
演示我的评论:
$parent = array(
array(//$parent[0]
"a-key",
"b-key" => array(
array("1", "2", "3"), //b-key[0]
array("4", "5", "6")) //b-key[1]
,
"c-key"),
array(//$parent[1]
"a-key",
"b-key" => array(array("7", "8", "9"), //b-key[0]
array("10", "16", //b-key[1]
"12" => array(array("13"), //12[0]
array("14"))), //12[1]
array("15", "16" => array(), "17")) //b-key[2]
,
"c-key"),
array(//$parent[2]
"a-key",
"b-key" => array(array("7", "8", "9"), //b-key[0]
array("10", "16", //b-key[1]
"12" => array(array("13"), //12[0]
array("14"))), //12[1]
array("15", "16" => array(), "17")) //b-key[2]
,
"c-key")
);
展示我的评论:
$parent = array(
array(//$parent[0]
"a-key",
"b-key" => array(
array("1", "2", "3"), //b-key[0]
array("4", "5", "6")) //b-key[1]
,
"c-key"),
array(//$parent[1]
"a-key",
"b-key" => array(array("7", "8", "9"), //b-key[0]
array("10", "16", //b-key[1]
"12" => array(array("13"), //12[0]
array("14"))), //12[1]
array("15", "16" => array(), "17")) //b-key[2]
,
"c-key"),
array(//$parent[2]
"a-key",
"b-key" => array(array("7", "8", "9"), //b-key[0]
array("10", "16", //b-key[1]
"12" => array(array("13"), //12[0]
array("14"))), //12[1]
array("15", "16" => array(), "17")) //b-key[2]
,
"c-key")
);
如果我正确地阅读了您的注释,那么您知道所有键,并且希望将元素添加到可以通过这些键访问的数组中 在这种情况下,您只需将其附加到该元素的末尾即可:
$ref = &$section;
while (is_array(end($ref))) {
$key = key($ref);
$ref = &$ref[$key];
}
array_push($ref, array('1','2','3'));
编辑:如果还有一层,您需要先获取$section
的最后一个元素,您可以使用以下方法:
$section['id1']['id2']['id3'][] = $the_array_you_want_to_add;
如果我正确地阅读了您的注释,那么您知道所有键,并且希望将元素添加到可以通过这些键访问的数组中 在这种情况下,您只需将其附加到该元素的末尾即可:
$ref = &$section;
while (is_array(end($ref))) {
$key = key($ref);
$ref = &$ref[$key];
}
array_push($ref, array('1','2','3'));
编辑:如果还有一层,您需要先获取$section
的最后一个元素,您可以使用以下方法:
$section['id1']['id2']['id3'][] = $the_array_you_want_to_add;
这是代码的“重做”,因为前面的答案没有达到任何人想要的效果 要求: 鉴于:
- 1) 一个
,例如根目录
$section[0]
- 2) 要添加的数据数组(
)$newData
- 3) 带有空数组的“entry”的键,用于将数据添加到其中
- 将当前空数组替换为
$newData
树
,我使用术语$node
来指代其中的条目
我如何选择接近它
1) 假设您有一个类
(TreeWalk),可以“访问”树中的每个节点
2) 它可以在节点上运行任何可调用的(访问者)
3) 该节点可由访问者修改
因此,假设TreeWalk
类存在,那么执行所需操作的方法是:
- 编写一个
访问者
,以识别所需的节点
,并对其进行更新
这就是这里提供的
我将提供“TreeWalk”类,但这是一个“general TreeWalk”类,用于运行将更新正确节点的“nodeProcessor”
资料来源和示范:
_更新:添加代码来执行OP要求的操作
代码(节点处理器):
1) 它使用的数据:
2) 要在树中的每个节点上运行的代码
运行代码:
TreeWalk接口:
这是代码的“重做”,因为前面的答案没有达到任何人想要的效果
要求:
鉴于:
- 1) 一个
根目录
,例如$section[0]
- 2) 要添加的数据数组(
$newData
)
- 3) 带有空数组的“entry”的键,用于将数据添加到其中
结果:
- 将当前空数组替换为
$newData
由于这是一个树
,我使用术语$node
来指代其中的条目
我如何选择接近它
1) 假设您有一个类
(TreeWalk),可以“访问”树中的每个节点
2) 它可以在节点上运行任何可调用的(访问者)
3) 该节点可由访问者修改
因此,假设TreeWalk
类存在,那么执行所需操作的方法是:
- 编写一个
访问者
,以识别所需的节点
,并对其进行更新
这就是这里提供的
我将提供“TreeWalk”类,但这是一个“general TreeWalk”类,用于运行将更新正确节点的“nodeProcessor”
资料来源和示范:
_更新:添加代码来执行OP要求的操作
代码(节点处理器):
1) 它使用的数据:
2) 要在树中的每个节点上运行的代码
运行代码:
TreeWalk接口:
end()
移动指针。。下一步是通过代码<代码>()<代码>获取密钥。您可以考虑首先遍历/遍历数组结构并返回指针/引用。然后使用引用。那么,在这种情况下,我可以使用哪种方法?我不明白,为什么要将数据推送到未知键(或一系列未知键…)上?这将使它更难取回/使用。密钥名是已知的。我想将数据插入到已知密钥中。我使用循环来插入数据。每次它都应该插入到特定键的最后一个元素。id1
是一个数组。它的索引应该在调用id2
之前提到,比如$section[0]['id1'][0]['id2'][0]['id3']
。我想这样,但举个例子,我提到了0作为索引。在0的位置,我希望数组的最后一个元素索引end()
移动指针。。下一步是通过代码<代码>()<代码>获取密钥。您可以考虑首先遍历/遍历数组结构并返回指针/引用。然后使用引用。那么,在这种情况下,我可以使用哪种方法?我不明白,为什么要将数据推送到未知键(或一系列未知键…)上?这将使它更难取回/使用。密钥名是已知的。我想将数据插入到已知密钥中。我使用循环来插入数据。每次它都应该插入到特定键的最后一个元素。id1
是一个数组。在调用像$sectio这样的id2
之前应该提到它的索引
$tree = new TreeWalk($parent, $updateNode, $updateData);
$tree->treeWalk();
<?php
/*
* The class can be found here: http://pastebin.com/wKuKrPTv
*/
Interface ITreeWalk {
/**
* This class processes `paths` is a tree made from arrays
*
* You can provide a 'nodeProcessor' which is amy `callable` that will be
* run against every node.
*
* Also available is the `current path` through the arrays to get to the `currentNode`.
*
*/
/**
* A reference to the current node so it can be updated
*
* @property mixed $currentNode
*/
/**
* The key of the currentData - readonly
*
* @propert mixed $currentKey
*/
/**
* The 'current' stack of nodes in this path to reach this `currentNode`
*
* This is a 'stack'. The 'path' is all the entries combined.
*
* Each path entry is: array('key' => $currentKey, 'data' => $currentNode)
*
* @property array $currentPath
*/
/**
* Available data to the nodeProcesor
*
* @property mixed $processorData
*/
/**
* The Output
*
* @property array $processorResults
*/
/**
* Terminate the processing of any further nodes
*
* @property boolean $endTreeWalk
*/
/**
* Build the class but do not run it...
*
* Provides a default NodeProcessor if you don't provide one.
* o The default processor builds string paths that look like array paths
*
* @param array $tree
* @param callable $processNode - optional
* @param processorData - optional
*/
// public function __construct(array &$tree,
// /* callable */ $processNode = null,
// /* mixed */ $processorData = null);
/**
* This routine makes this class rather useful as you can use any callable.
*
* The nodeProcessor accepts one parameter:
* o the instance of the 'TreeWalk'
*
* It has some useful items of data available to it:
* 1) The `currentNode` - this is a reference so the node can be updated
* 2) The `currentKey` - this is the `array index' of the 'currentNode'
*
* 3) The `processorData` - This is anything that you decide you need when processing
* the `currentNode`.
*
* 4) The full path that leads to this node.
* This is useful as it can be checked as part of the search conditions
* if you wish.
*
* @param callable $nodeProcessor
*/
public function setNodeProcessor(/* callable */ $nodeProcessor);
/**
* This is the data used by the `nodeProcessor` it can be anything.
*
* Normally, an array?
*
* @param mixed $data
* @return void
*/
public function setProcessorData($data);
/**
* Any walue returned from the 'nodeProcessor' will be stored added to this array.
*
* Return a list of results were generated by the 'nodeProcessor'
* @return array
*/
public function results();
/**
* Process all the nodes.
*
* @param mixed optional - The data to be used by the `nodeProcessor`
*
* @return void
*/
public function treeWalk($processorData = null);
/**
* show the current path as text
*
* What if you don't use `foreach` - is it easier to understand? It is for me...
*
* Path stucture:
* 1) root node (required)
* 2) an iteration of intermediate nodes (optional) - makes sense
* 3) end data node (optional) !
* - what? yes - what if path is just the root ;-/
*
* @return string
*/
public function showCurrentPath();
/**
* If you don't provide a callable to generate paths then this will be used.
*
* It generates a list string paths to all the leaf nodes.
*
* @return string
*/
// public function defaultNodeProcessor();
}