从多维数组
问题描述:
假设我有这个数组返回值(实际是一个大得多确实):从多维数组
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循环,但我想要正确地搜索正确的数组索引值。重复问题中提到的功能是我所熟知的,但在这种情况下不适用于性能。数组大小太大每个脚本运行
答
时间做一个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
)
)
...
我正在寻找array_search中的$ periodo。并且在本例中它是2013年3月。我想要的密钥对[periodo] => '03/2013' –
可能重复[PHP多维数组搜索](http://stackoverflow.com/questions/6661530/php-multi-dimensional-array-search ) – Sean
@Sean两者是如何相关的?它是在第一级数组的单个项目中搜索单个值。每次我必须搜索时,我不希望每次都使用foreach。 Periodo是一个日期,从月开始从2008年1月到2017年12月每个月(表列),我有100或更多行每次(每个是CD000 ...)我使用类似于该答案中的代码对于其他脚本,但在这里我想看看我的代码有什么问题! –