【问题标题】:Merge multiple array to single array将多个数组合并为单个数组
【发布时间】:2015-09-28 17:13:07
【问题描述】:

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

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

谁能告诉我如何做到这一点

【问题讨论】:

  • 你能用你想看到的示例输出更新你的问题吗?您想要数组中有多个 id 值,还是想要一个带有一组值的 id 条目?
  • @Tim Biegeleisen 更新了我的帖子
  • 一次重构:如果您只需要第一个查询中的userid,为什么不能将您的第一个查询更改为"SELECT userid FROM interest where interest='".$interest."' and userid!='".$myuserid."'"

标签: mysql sql arrays multidimensional-array


【解决方案1】:

在代码开头创建一个数组变量,例如 $a=array();

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

print_r($a);

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 虽然我得到了答案,但你能告诉我为什么不能在循环中打印结果
    • 您在循环中得到了结果,但它显示了单个数组记录,您希望整个结果在单个数组中,这就是为什么需要创建 $user_data 变量
    • 在这篇文章中,我有一部分代码,在这里我只显示了 if($searchtype == 'both') 的一个条件,但我还有 2 个基于 searchtype 的条件,我希望分别打印每个条件的结果,但是如果我在外面打印结果,我将无法根据条件获得结果
    • 如果你有已发布问题的答案,那么你可以在新帖子中提出新问题,最好给出最佳答案
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-11
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      相关资源
      最近更新 更多