繁体   English   中英

从多维数组中返回一个值

[英]return a value from a multidimensional array

假设我有这个数组(真正的数组确实更大):

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次

$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
                )

        )
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM