Lelio Faieta
Lelio Faieta

Reputation: 6679

return a value from a multidimensional array

Assume I have this array (the real one is much bigger indeed):

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
                )

        )
)

I'm trying to get the value of [incasso] and [spesa] that match first level of array and [periodo] on the second level. So for example I look for CD000000002 and if I find it then I look for 03/2013. If I find it I'd like to return [incasso] and [spesa] values. Both CD000000002 and [periodo] are built from for loops so I'll test existing and not existing values. Actually it seems to me that i cannot access the second array correctly and I don't understand why. This is my actual code: ($credito in the example is 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);

What am I doing wrong??? I don't want to use a foreach loop but I want to search for the exact value addressing the array indexes correctly. The function mentioned in the duplicated question is well known to me but is not applicable in this case for performance. Array size are too big to do a foreach 5.000 times at least each time the script runs

Upvotes: 1

Views: 59

Answers (1)

Sean
Sean

Reputation: 12433

in order for $key = array_search($periodo,$flussi[$credito]); to find the value of periodo, you need to change your array from numerical keys

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
                )

        )
...

to an array where the periodo value is the key

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
                )

        )
...

Upvotes: 3

Related Questions