0

I can't see where I am wrong with this code so I kindly ask for your help. I have two arrays:

Array (
    [0] => Array (
        [description] => Generali di Proprieta'
        [idmov] => 34
        [mov] => Manutenzioni
        [total] => 8000
    )
    [1] => Array (
        [description] => Generali di Proprieta'
        [idmov] => 35
        [mov] => Assicurazioni
        [total] => 6000
    )
    [2] => Array (
        [description] => Generali di Proprieta'
        [idmov] => 36
        [mov] => Cancelleria Postali
        [total] => 1850
    )
    [3] => Array (
        [description] => Generali di Proprieta'
        [idmov] => 37
        [mov] => Bancarie passive
        [total] => 700
    )
    [4] => Array (
        [description] => Generali di Proprieta'
        [idmov] => 38
        [mov] => Amministrazione
        [total] => 15000
    )
)

and

Array (
    [0] => Array (
        [center] => 8
        [caus] => 34
        [total] => 38175.04
    )
    [1] => Array (
        [center] => 8
        [caus] => 35
        [total] => 6132.00
    )
    [2] => Array (
        [center] => 8
        [caus] => 36
        [total] => 223.80
    )
    [3] => Array (
        [center] => 8
        [caus] => 37
        [total] => 114.70
    )
    [4] => Array (
        [center] => 8
        [caus] => 38
        [total] => 14625.07
    )
    [5] => Array (
        [center] => 8
        [caus] => 39
        [total] => 7450.48
    ) 

I use this function

function searchForId($id, $array) {
   foreach ($array as $key => $val) {
       if ($val['caus'] === $id) {
           return $key;
       }
   }
   return null;
}

to look in array B for each item of array A with this code:

for($i=0;$i<$length;$i++){
    if(searchForId($voce_bdg[$i]['idmov'], $voce_actual)){
        $key=searchForId($voce_bdg[$i]['idmov'], $voce_actual);
        $actual=$voce_actual[$key]['importo'];
        echo '<td class="report">'.number_format($actual,2,',','.').'</td>';
     }else{
        echo '<td class="report">0,00</td>';
    }
}

It works for every item like a charm except for the first item where it returns 0.

Where am I wrong??

Thanks in advance for your help! Lelio

3
  • Name variables in English that will make it more understandable for others. Commented Oct 21, 2013 at 15:30
  • 1
    AAAAAAHHHHH! I formatted your question readably, why did you undo it? Commented Oct 21, 2013 at 15:36
  • Sorry, I just edited the variables names. Probably we did it concurrently :( Commented Oct 21, 2013 at 15:40

3 Answers 3

1

PHP treats the index 0 as a false. As such, if you find your result in index zero, it won't pass the if() statement you have.

Since your function returns null if no record found, why not try to check for null?

for($i = 0; $i < $length; $i++)
{
    // Use is_null() check below. If it is not null, it is found.
    // Also, instead of doing searchForId() twice, just do it once and check for the result.

    $key = searchForId($voce_bdg[$i]['idmov'], $voce_actual);

    if(! is_null ($key))
    {
        $actual = $voce_actual[$key]['importo'];
        echo '<td class="report">'.number_format($actual,2,',','.').'</td>';
    }
    else
    {
        echo '<td class="report">0,00</td>';
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

It works, thank you. "PHP treats the index 0 as a false. As such, if you find your result in index zero, it won't pass the if() statement you have." Never realized before! Thank you again!
if this works then its fine other wise you can simply use in_array(searchitem,arraylist). put what item from array 1 you want to search in param 1 and put array 2 in param 2. php.net/manual/en/function.in-array.php
@dishwasherWithProgrammingSkill You cannot use in_array() in this case, because: 1. The second array is 2-dimensional; 2. We want to look at the inner array with key 'caus'; 3. We want to retrieve the key where the search item is found to be used later.
my bad didn't realize it was a multidimensional array, guess I should stick to dishwashing ;D
0

try replacing operator === for ==

1 Comment

Already tried. No changes. The first idmov=34 that match caus=34 provides no results. All the following are ok both with === and ==
0

It does return something. It return 0 since the key is 0. But your if() interpret it as a "false"

change

if(searchForId($voce_bdg[$i]['idmov'], $voce_actual)){

with

if(searchForId($voce_bdg[$i]['idmov'], $voce_actual) != null){

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.