2016-07-25 62 views
2

我想知道如何在數組b1和b2中動態添加數組,因爲如果條件被調用超過一次,那麼b1會覆蓋自身,b2也是如此。動態數組再次覆蓋

$sql = "SELECT senderid , receiverid,request FROM friendrequest WHERE request = '1'"; 
    $result = mysqli_query($con,$sql); 
    $u1 = array(); 
    $u2 = array(); 
    $b1 = array(); 
    $b2 = array(); 


while ($row =mysqli_fetch_array($result)){ 

    if(($row['senderid']==$senderid)){ 
     $value = $row['receiverid']; 
     $u1['receiverid']=$value; 
     $b1 = array($u1['receiverid']);  

    }else if(($row['receiverid']==$senderid)){ 
     $value = $row['senderid']; 
     $u2['senderid']=$value; 
     $b2 = array($u2['senderid']); 

    } 

    $b=array_merge($b1,$b2); 
} 

我也嘗試了另一個邏輯代碼,但它給了我錯誤。

$sql = "SELECT senderid , receiverid,request FROM friendrequest WHERE request = '1'"; 
$result = mysqli_query($con,$sql); 
$u1 = array(); 
$u2 = array(); 
$b1 = array(); 
$b2 = array(); 
$f1=0; 
$f2=10; 

while ($row =mysqli_fetch_array($result)){ 
    $f1=$f1+1; 
    if(($row['senderid']==$senderid)){ 
     $value = $row['receiverid']; 
     $u1['receiverid']=$value; 
     $b1[$f1] = array($u1['receiverid']);  

    }else if(($row['receiverid']==$senderid)){ 
     $value = $row['senderid']; 
     $u2['senderid']=$value; 
     $b2[$f1] = array($u2['senderid']); 

    } 

    $b=array_merge($b1,$b2); 
} 
+0

不,它不工作 – Shubham

+1

你想最終的陣列看起來像什麼? – bloodyKnuckles

+0

人們沒有迴應。 –

回答

0

變化

$b1 = array($u1['receiverid']);  
$b2 = array($u2['senderid']); 

$b1[] = array($u1['receiverid']);  
$b2[] = array($u2['senderid']);