簡體   English   中英

將多個數組合並為單個數組

[英]Merge multiple array to single array

我有這段代碼,我希望得到一個包含所有值的單個數組。

$sql = "SELECT * FROM interest where interest='".$interest."' and userid!='".$myuserid."'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) 
    {
        while($row = mysqli_fetch_assoc($result)) 
            {
                $userid = $row["userid"];

                if($searchtype == 'both')
                    {
                        $sql2 = "SELECT * FROM register where id='".$userid."' and  discover = 'on' and id!='".$myuserid."'";
                        $result2 = mysqli_query($conn, $sql2);
                        if (mysqli_num_rows($result2) > 0) 
                            {
                                while($row2 = mysqli_fetch_assoc($result2)) 
                                    {
                                        echo "<pre>";
                                        print_r($row2);
                                        echo "</pre>";
                                    }
                            }       
                    }
            }
    }

我得到的o / p是這樣的

Array
(
    [id] => 1
    [email] => A1
    [username] =>B1 
    [password] => C1
    [gender] => C1
)

Array
(
    [id] => 2
    [email] => A2
    [username] => B2
    [password] => C2
    [gender] => D2
)
Array
(
    [id] => 3
    [email] => A3
    [username] => B3
    [password] => C3
    [gender] => D3
)

但我希望將所有數據都放在這樣的單個數組中

Array
(
    [0] => Array
        (
             [id] => 1
             [email] => A1
             [username] =>B1 
             [password] => C1
             [gender] => C1
        )

    [1] => Array
        (
            [id] => 2
            [email] => A2
            [username] => B2
            [password] => C2
            [gender] => D2
        )
     [2] => Array
        (
            [id] => 3
            [email] => A3
            [username] => B3
            [password] => C3
            [gender] => D3
        )
}

任何人都可以告訴我怎么能這樣做

創建一個數組變量,如$a=array(); 在代碼的開頭

獲取數組中的行值$ a [] =您的行值(while循環),然后打印此外部循環您將獲得單個數組打印中的所有值

print_r($a);

在循環開始之前取一個數組變量,如$user_data = array(); 在內循環中你必須設置$user_data[] = $row2;

if (mysqli_num_rows($result) > 0) {
    $user_data = array();
    while($row = mysqli_fetch_assoc($result)) {
            $userid = $row["userid"];
            if($searchtype == 'both') {
                    $sql2 = "SELECT * FROM register where id='".$userid."' and  discover = 'on' and id!='".$myuserid."'";
                    $result2 = mysqli_query($conn, $sql2);
                    if (mysqli_num_rows($result2) > 0) {
                            while($row2 = mysqli_fetch_assoc($result2)) {
                                    $user_data[] = $row2;
                                }
                        }       
                }
        }
   print_r($user_data);   //Print here your user_data outside the loop.
}

您幾乎接近目標,只需定義一個數組並將每行的數據保存在其中

// array which contains all rows data
$all_rows = array();                                //  <------ step 1
if(mysqli_num_rows($result2)) 
{
   while($row2 = mysqli_fetch_assoc($result2)) 
   {
      // every record data is stored here
      $all_rows[] = $row2;                          //  <------ step 2
   }
} 
if(!empty($all_rows))
{
      print_r($all_rows);
}else
{
     echo "do some thing else since zero records fetched";
}  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM