如何通过引用动态设置多维数组中的值 [英] How to dynamically set value in multidimensional array by reference
问题描述
这让我整个晚上都发疯了.基本上,我需要在清理值后在多维数组中设置特定值,然后在验证值后再次设置(可能取决于验证;如果验证失败则需要将值设置为空字符串).假设我有这个帖子数组:
$data['should']['be']['int'] = 'yjd';
使用 filter_var( $value, FILTER_SANITIZE_NUMBER_INT );
清理值后,我得到一个空字符串.然后我需要以某种方式将 $data['should']['be']['int']
上的值设置为空字符串.
这个值然后被传递给一个验证函数,它失败了,因为空字符串不是一个整数.同样,该验证值需要在 $data['should']['be']['int']
中设置为空字符串.
在整个验证开始之前,我将所有相关的键保存在一个数组中,所以当我需要清理或验证时,我已经有了这样的东西:
$keys = 数组(0 =>'应该',1 =>'是',2 =>'int');
我尝试通过引用 &$data
数组来设置新值,然后在 foreach 循环中使用上述键访问 $data
数组,但无论我尝试什么,都无法做到.以上只是一个简化的例子.整个事情是验证类的一部分,所以我不知道传递的 $data
数组的确切深度.
任何指针将不胜感激!感谢您的帮助!
以为我无法编辑帖子,但它最终只是我的互联网连接.请无视我下面的评论.无论如何,这是我尝试递归调用的方法:
受保护的函数 set_value( &$data, $value ) {foreach( $data as &$val ) {if( is_array( $val ) ) {$this->set_value( $val, $value );} 别的 {$val = $value;}}}
为了开始循环,我这样做了:
$this->set_value( $data[$keys[0]], $value );
你可以试试
$data = array();$keys = 数组(0 =>'应该',1 =>'是',2 =>'int');$value = 'yjd';echo "";setValue($data,$keys,$value);打印_r($数据);
输出
数组([应该] =>大批([是] =>大批([int] =>yjd)))
使用的函数
function setValue(&$data, $path, $value) {$temp = &$data;foreach ( $path 作为 $key ) {$temp = &$temp[$key];}$temp = $value;返回 $value ;}
This has been driving me nuts all evening. Basically, I need to set a specific value in a multidimensional array after sanitizing the value and then again (maybe, depends on validation; if validation failed then the value needs to be set to an empty string) after validating the value. Let's say I have this post array:
$data['should']['be']['int'] = ' yjd';
After sanitizing the value with filter_var( $value, FILTER_SANITIZE_NUMBER_INT );
I'm getting an empty string back. I would then need to somehow set the value on $data['should']['be']['int']
to be an empty string.
This value then gets passed to a validation function, which fails, cause the empty string is not an integer. Again, that validated value would then need to get set in $data['should']['be']['int']
to an empty string.
Before the whole validation thing kicks off I'm saving all relevant keys in an array, so by the time I need to sanitize or validate I've got something like this available:
$keys = array(
0 => 'should',
1 => 'be',
2 => 'int'
);
I've tried to then access the $data
array using the above keys in a foreach loop by referencing the &$data
array to set the new value, but haven't been able to, no matter what I tried. The above is just a simplified example. The whole thing is part of a validation class, so I don't know the exact depth of the passed $data
array.
Any pointers would be greatly appreciated! Thanks for your help!
Edit: Thought I couldn't edit the post, but it ended up just being my internet connection. Please disregard my comment below. Anyways, here is a method that I tried calling recursively:
protected function set_value( &$data, $value ) {
foreach( $data as &$val ) {
if( is_array( $val ) ) {
$this->set_value( $val, $value );
} else {
$val = $value;
}
}
}
To start the loop off I did this:
$this->set_value( $data[$keys[0]], $value );
You can try
$data = array();
$keys = array(
0 => 'should',
1 => 'be',
2 => 'int'
);
$value = 'yjd';
echo "<pre>";
setValue($data,$keys,$value);
print_r($data);
Output
Array
(
[should] => Array
(
[be] => Array
(
[int] => yjd
)
)
)
Function Used
function setValue(&$data, $path, $value) {
$temp = &$data;
foreach ( $path as $key ) {
$temp = &$temp[$key];
}
$temp = $value;
return $value ;
}
这篇关于如何通过引用动态设置多维数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!