1

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

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
  • I am looking for $periodo in array_search. And $periodo it's 03/2013 in the example. I want the key that corrispond to [periodo] => '03/2013' – Lelio Faieta Jun 20 '15 at 14:00
  • possible duplicate of [PHP multi dimensional array search](http://stackoverflow.com/questions/6661530/php-multi-dimensional-array-search) – Sean Jun 20 '15 at 14:03
  • @Sean how the two are related? It's a search of a single value in a single item of the first level array. I don't want by any chance use a foreach each time i have to search. Periodo is a date and start from 01/2008 to 12/2017 month by month (table columns) and I have 100 or more rows each time (each is a CD000...) I use a code similar to the one in that answer for other scripts but here I'd like to see what is wrong with my code! – Lelio Faieta Jun 20 '15 at 14:06
  • 1
    The value you're searching for in the level1 array, is an array itself. – Radu Andrei Jun 20 '15 at 14:11
  • @RaduAndrei yes. And? What is wrong with that? I build this array in another script – Lelio Faieta Jun 20 '15 at 14:12
  • You expect your code - `$key = array_search($periodo,$flussi[$credito]);` to find the parent key, where the child value of `'periodo'` is `$periodo`, but that is not how [`array_search()`](http://php.net/manual/en/function.array-search.php) works. It gives you the key of that value, not a one level deeper value, ie. `array(1=>'03/2013')` would return `1`, but `array(1=>array('periodo'=>'03/2013'))` would return `null` as `array_search()` does not search levels deep. If you don't want to loop the array, you will need to find some other way to transverse your array. – Sean Jun 20 '15 at 14:12
  • @Sean so I should do a foreach($flussi[$credito]) and see if I find $flussi[$credito][$periodo] ? – Lelio Faieta Jun 20 '15 at 14:16
  • Presuming your search term in the level 1 array is an array (like it should be, if you have array in array), then there's nothing wrong with that, but if it finds a match, it returns the key in the level 1 array - which does not help you to go deeper. – Radu Andrei Jun 20 '15 at 14:18
  • Yes that could work. Also, are you able to change/modify your array structure? Instead of using numbered arrays -> `0=>array(), 1=>array(), ....`, you could build it with the `'periodo'` value as the key -> `'11/2010'=>array(), '3/2013'=>array(), ....`, then you would not have to loop, just access it by the key. – Sean Jun 20 '15 at 14:19
  • @Sean you are a genius! I build the array structure from a flat array returned by the query. Just adding the second level will work like a charm! Please post this as an answer :) I solved only half of my problem and didn't see that the same solution could solve the other half – Lelio Faieta Jun 20 '15 at 14:22

1 Answers1

3

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
                )

        )
...
Sean
  • 12,443
  • 3
  • 29
  • 47