假设我有这个数组(真正的数组确实更大):
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
)
)
)
我试图获得匹配第一级数组的[incasso]和[spesa]以及第二级匹配[periodo]的值。所以例如我寻找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次
答案 0 :(得分:3)
为了$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
)
)
...