2013-10-21 75 views
0

我看不到這個代碼的錯誤,所以我懇求您的幫助。 我有兩個陣列:在數組中找不到價值

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

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 
    ) 

我使用此功能

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

看數組B中與此代碼數組A中的每個文件:

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>'; 
    } 
} 

它的工作原理對於每一個項目都像一個魅力,除了它返回0的第一個項目。

我在哪裏錯了?

在此先感謝您的幫助! Lelio

+0

英文名稱變量會使其他人更容易理解。 – Leri

+1

AAAAAAHHHHH!我可讀的格式化你的問題,你爲什麼解開它? – Barmar

+0

對不起,我剛剛編輯了變量名稱。可能我們同時做了:( –

回答

1

PHP對待指數0作爲false。因此,如果您在索引零中找到您的結果,它將不會通過您擁有的if()聲明。

既然你的函數返回null如果沒有找到記錄,爲什麼不嘗試檢查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>'; 
    } 
} 
+0

它可以工作,謝謝。將索引0視爲false。因此,如果您在索引0中找到您的結果,它將不會通過您擁有的if()語句。「以前從未意識到!再次感謝您! –

+0

如果這樣工作,那麼它的好其他明智的,你可以簡單地使用in_array(searchitem,arraylist)。把你想在參數1中搜索的數組1中的項目放入參數2中。將數組2放入參數2中。 http://php.net/manual/en/function.in- array.php – dishwasherWithProgrammingSkill

+0

@dishwasherWithProgrammingSkill在這種情況下,你不能使用'in_array()',因爲:1.第二個數組是二維的; 2.我們想看看帶有'caus'鍵的內部數組; 3.我們想要檢索搜索項後被發現使用的關鍵字 – Sutandiono

0

嘗試更換運營商===爲==

+0

已經嘗試過,沒有變化,第一個匹配caus = 34的idmov = 34沒有提供任何結果,以下所有都可以使用===和== –

0

它確實有返回值。它返回0,因爲關鍵是0,但如果你()把它解釋爲 「假」

變化

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

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