我正在尝试编写一个采用多维数组的片段,并在找到命名搜索键的同一级别插入一些键。 我不必依赖阵列的结构(但最多只能有5个级别) 我无法通过引用传递,因此传统的重复函数不会对该方法有所帮助。
我有两个选项:SPL或重新构造数组并沿途改变它的递归
使用SPL我似乎无法插入新值..
$a= new \ArrayObject($priceConfig);
$array = new \RecursiveArrayIterator($a);
$iterator = new \RecursiveIteratorIterator($array, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $key => $value) {
if (is_array($value) && $key == 'prices') {
$iterator->offsetSet('myPrice',['amount'=>'1.00']);
}
}
print_r($a->getArrayCopy());
它不会将新密钥插入所需的级别,但它会循环遍历数组..我缺少什么?
重构数组并在嵌套数组中的键搜索中插入新值的递归函数有效,但我想使用迭代器来做这个...
function recursive( $input, $searchKey, $key=null) {
$holder = array();
if(is_array( $input)) {
foreach( $input as $key => $el) {
if (is_array($el)) {
$holder[$key] = recursive($el, $searchKey, $key);
if ($key == $searchKey) {
$holder[$key]['inertedPrice'] = "value";
}
} else {
$holder[$key] = $el;
}
}
}
return $holder;
}
INPUT(总是会有一些"价格键和结构在X级")
[1] => Array
(
[1] => Array
(
[prices] => Array
(
[onePrice] => Array( [amount] => 10)
[finalPrice] => Array ([amount] => 10)
)
[key1] => value2
[key2] => value2
)
[2] => Array
(
[prices] => Array
(
[otherPrice] => Array([amount] => 20)
[finalPrice] => Array([amount] => 20)
)
[key] => value
)
)
)
输出
[1] => Array
(
[1] => Array
(
[prices] => Array
(
[onePrice] => Array( [amount] => 10)
[finalPrice] => Array ([amount] => 10)
[INSERTEDPrice] => Array([amount] => value)
)
[key1] => value2
[key2] => value2
)
[2] => Array
(
[prices] => Array
(
[otherPrice] => Array([amount] => 20)
[finalPrice] => Array([amount] => 20)
[INSERTEDPrice] => Array([amount] => )
)
[key] => value
)
)
)
答案 0 :(得分:2)
您可以使用自定义迭代器逻辑扩展RecursiveArrayIterator
来使用迭代器执行此操作:
class Foo extends RecursiveArrayIterator
{
public function getChildren()
{
if ($this->key() == 'prices') {
return new self(array_merge($this->current(), ['foo' => 'bar']));
} else {
return parent::getChildren();
}
}
}
答案 1 :(得分:1)
您可以使用基本工具(如foreach循环和递归)轻松解决问题。这是一个解决方案。
function mergeWithKey($targetKey, $new, array $array) {
foreach ($array as $key => $value) {
if ($key === $targetKey) {
$array[$key] = array_merge($array[$key], $new);
}
elseif (is_array($value)) {
$array[$key] = mergeWithKey($targetKey, $new, $value);
}
}
return $array;
}
// Example
$output = mergeWithKey('prices', array('INSERTEDPrice' => 'value'), $input);
简单地说,当我们找到我们正在寻找的密钥时,我们迭代数组,然后我们合并新价格。如果我们找到一个子数组,那么我们将新价格合并到该子数组中。
我努力通过将关键“价格”作为参数来概括功能。这可能仍然是一个不太可能看到重用的难以理解的功能。
使用几种常见的算法可以很好地提升这个功能,而且你可以重用这些算法。一种是使用键和值映射数组,另一种是将2D数组展平为1D数组。
function arrayMapWithKey(callable $f, array $array) {
$out = array();
foreach ($array as $key => $value) {
$out[$key] = $f($key, $value);
}
return $out;
}
function concat(array $array) {
if (empty($array)) {
return array();
}
else {
return call_user_func_array('array_merge', $array);
}
}
这些定义使您能够编写替代解决方案。
function addPrice($name, $price, $data) {
return concat(arrayMapWithKey(
function ($k, $v) use ($name, $price) {
if ($k === 'prices') {
return array($k => array_merge($v, array($name => $price)));
}
elseif (is_array($v)) {
return array($k => addPrice($name, $price, $v));
}
else {
return array($k => $v);
}
},
$data
));
}
arrayMapWithKey
的另一个表述是使用值复制键,然后使用常规array_map
。
function arrayWithKeys(array $array) {
$out = array();
foreach ($array as $key => $value) {
// in PHP arrays are often used as tuples,
// and here we have a 2-tuple.
$out[] = array($key, $value);
}
return $out;
}