PHP MYSQL 多维数组

标签 php mysql

我在尝试从两个单独的 MySQL 选择创建多维数组时感到非常头疼....我一直在这里和谷歌搜索一整天,最后不得不承认失败并寻求一些帮助(我是新手也没有帮助!!!)。

我有两个表,一个包含每个 id 的单行结果,另一个包含一个 id 的多行。我要做的是将两者组合成一个多维数组。

我的代码(可能很糟糕)如下所示:

require 'php/phpConnection.php';

$sqlString1 = mysql_query("SELECT id FROM supportstaff_section1_a");

$firstArray = array();
$secondArray = array();

while ($r = mysql_fetch_assoc($sqlString1)) {
    $applicantID = $r['id'];
    $sqlString2 = mysql_query("SELECT educationalname FROM supportstaff_section5 WHERE id = '$applicantID'");

    while ($x = mysql_fetch_assoc($sqlString2)) {
        $secondArray[] = $x;
    }
    $firstArray[] = $r + $secondArray;
    $secondArray  = array();
}
print json_encode($firstArray);
mysql_close($con);

结果是这样的:

[{"id":"8m8wwy","0":{"educationalname":"GCSE - English"},"1":{"educationalname":"GCSE - Maths"}},{"id":"wiL7Bn"},{"id":"zAw6M1"}]

但我认为它需要看起来像这样:

[{"id":"8m8wwy","Array2":"[{"educationalname":"GCSE - English"},{"educationalname":"GCSE - Maths"}]"},{"id":"wiL7Bn"},{"id":"zAw6M1"}]

无论如何,如何将我的第二个 SQL Select 插入到每个 ID 的第一个 SQL Select 中。

感谢您的任何建议/帮助。

编辑

取自 W3Schools.com:

Array
(
    [Griffin] => Array
    (
    [0] => Peter
    [1] => Lois
    [2] => Megan
    )
[Quagmire] => Array
    (
    [0] => Glenn
    )
[Brown] => Array
    (
    [0] => Cleveland
    [1] => Loretta
    [2] => Junior
    )
)

我正在努力让它像上面那样工作。

最佳答案

您需要在这里发挥一点创意。像下面这样的东西可以作为连接 AND 与多维数据一起工作:

<?php
  require 'php/phpConnection.php';

  // ======================================================================
  // Create a join query (way faster than several separate ones!)
  $sqlquery =
    "SELECT SSSA.id, SSS5.educationalname" .
    " FROM supportstaff_section1_a SSSA" .
      " LEFT OUTER JOIN supportstaff_section5 SSS5 ON SSS5.id = SSSA.ID";


  // ======================================================================
  // Run the query and get our results
  $resultarray = array();
  if ($resource = mysql_query($sqlquery)) {
    while ($curarray = mysql_fetch_assoc($resource)) {
      // Create an array, if it doesn't exist
      if (!isset($resultarray[$curarray["id"]]))
        $resultarray[$curarray["id"]] = array();

      // Add to the array, if not null
      $curstring = (string) $curarray["educationalname"];
      if ($curstring != "")
        $resultarray[$curarray["id"]][] = $curstring;
    }
    mysql_free_result($resource);
  }


  // ======================================================================
  // Convert from a keyed array to a standard indexed array (0, 1, 2, etc.)
  $finalarray = array();
  foreach ($resultarray as $id => & $data) {
    // Start with just ID
    $newarray = array(
      "id" => $id
    );

    // Get the data, if we have any
    if (count($data))
      $newarray["educationalnames"] = & $data;

    // Add to our final array and clear the newarray
    $finalarray[] = & $newarray;
    unset($newarray);
  }


  // ======================================================================
  // Get the JSON of our result
  $jsonresult = json_encode($finalarray);


  // ======================================================================
  // Echo it to test
  echo $jsonresult;


  // ======================================================================
  // Close the database
  mysql_close($con);
?>

生成的 $jsondata 看起来像这样(但当然不是那么解开):

[
  {
    "id": "8m8wwy",
    "educationalnames": ["GCSE - English", "GCSE - Maths"]
  },
  {
    "id": "wiL7Bn"
  },
  {
    "id": "zAw6M1"
  }
]

关于PHP MYSQL 多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15045385/

相关文章:

php - Doctrine2 复杂的统计查询

javascript - 从语言文件加载时缺少一半字符串

php - 不同页面的字体大小变化

php - 更新包含数百万行的 MySQL 表

php - 如何将 mysql 数据类型浮点更改为逗号?

java - JPQL 比较运算符的占位符

mysql - 如何查找最近一小时内添加或更新的 MySQL Views 或 Triggers?

php - 在使用 php 成功进行 paypal 货币交易后提交表单

javascript - 将日期转换为毫秒至今

mysql - 如何在一个查询中使用两个 LEFT JOIN?