将多个阵列合并为单个阵列



我有这段代码,我希望从中获得包含所有值的单个数组。

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

在while循环开始前取一个数组变量,如$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";
}  

相关内容

  • 没有找到相关文章

最新更新