2013-10-21 34 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){