2

我在尝试从两个单独的 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
    )
)

我试图让它像上面那样工作。

4

2 回答 2

5

你需要在这里有点创意。像下面这样的东西可以作为一个连接和多维数据:

<?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"
  }
]
于 2013-02-23T21:01:30.777 回答
1

如果您有来自第一个数组的 ID,则可以在第二个数组中检查具有此 ID 的键/值。

如果你想得到你应该使用的密钥

array_key_exists($string)

如果你想获得你应该使用的价值

in_array($string)

您可以使用 foreach 循环来执行此功能!

于 2013-02-23T20:46:22.557 回答