假设我有这个数组(真正的数组确实要大得多):
Array
(
[CD000000001] => Array
(
[0] => Array
(
[periodo] => 10/2010
[incasso] => 15000.00
[spesa] => 0.00
)
[1] => Array
(
[periodo] => 03/2013
[incasso] => 0.00
[spesa] => 280.00
)
)
[CD000000002] => Array
(
[0] => Array
(
[periodo] => 11/2010
[incasso] => 327199.83
[spesa] => 0.00
)
[1] => Array
(
[periodo] => 03/2013
[incasso] => 0.00
[spesa] => 3194.90
)
)
)
我正在尝试获取与第一级数组和第二级 [periodo] 匹配的 [incasso] 和 [spesa] 的值。例如,我寻找CD000000002,如果我找到它,那么我寻找 03/2013。如果我找到它,我想返回 [incasso] 和 [spesa] 值。CD000000002 和 [periodo] 都是从 for 循环构建的,所以我将测试现有值而不是现有值。实际上,在我看来,我无法正确访问第二个数组,我不明白为什么。这是我的实际代码:(示例中$credito CD000000002):
if(isset($flussi[$credito])){
//if I find CD000000002
$key = array_search($periodo,$flussi[$credito]);
//return the key of the second level array that have the value 03/2013
if($key){
$incasso = $flussi[$credito][$key]['incasso'];
}else{
$incasso = 0.00;
//return the value of [incasso] corresponding to that key
}else{
$incasso = '0.00';
}
unset($key);
我做错了什么???我不想使用 foreach 循环,但我想正确搜索寻址数组索引的确切值。重复问题中提到的功能对我来说是众所周知的,但不适用于这种情况的性能。数组大小太大,无法在每次脚本运行时至少执行 5.000 次
>为了让$key = array_search($periodo,$flussi[$credito]);
找到periodo
的值,你需要从数字键更改数组
Array
(
[CD000000001] => Array
(
[0] => Array
(
[periodo] => 10/2010
[incasso] => 15000.00
[spesa] => 0.00
)
[1] => Array
(
[periodo] => 03/2013
[incasso] => 0.00
[spesa] => 280.00
)
)
...
到periodo
值为键的数组
Array
(
[CD000000001] => Array
(
[10/2010] => Array
(
[periodo] => 10/2010
[incasso] => 15000.00
[spesa] => 0.00
)
[03/2013] => Array
(
[periodo] => 03/2013
[incasso] => 0.00
[spesa] => 280.00
)
)
...