将多个数组合并为单个数组

时间:2015-07-10 04:37:21

标签: mysql sql arrays multidimensional-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
        )
}

任何人都可以告诉我如何做到这一点

3 个答案:

答案 0 :(得分:1)

在代码的开头创建一个类似$a=array();的数组变量

获取数组中的行值$ a [] =您的行值(while循环),然后打印此外部循环,您将获得单个数组打印中的所有值,如

print_r($a);

答案 1 :(得分:0)

在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.
}

答案 2 :(得分:0)

你几乎接近你的目标,你只需要定义一个数组并将每行的数据保存在其中

// 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";
}