我知道我的问题看起来有点不可理解但在这里我会解释一下。
我有3张桌子,这里是
我在这里有一个PHP代码,输出是基于查询创建一个数组,在这里。
<?php
require 'conn.php';
//Array Collectors
$arr_tblsrce1 = array();
$arr_tblsrce2 = array();
$arr_tbldest = array();
//Main Query
$rs_tbldest = mysqli_query($con, 'SELECT appkey FROM tbl_destination');
while ($rw_tbldest = mysqli_fetch_assoc($rs_tbldest)) {
$arr_tbldest[] = $rw_tbldest['appkey'];
}
//Do the Following procedures during the loop of Main Query Array
//for ($i=0; $i < count($arr_tbldest); $i++) {
foreach ($arr_tbldest as $main_appkey) {
//Create an array from tbl_source1
$rs_tblsrce1 = mysqli_query($con,"SELECT name FROM tbl_source1 WHERE appkey = '" .$main_appkey. "'");
while ($rw_tblsrce1 = mysqli_fetch_assoc($rs_tblsrce1)) {
$arr_tblsrce1[] = $rw_tblsrce1['name'];
}
//Create an array from tbl_source2
$rs_tblsrce2 = mysqli_query($con,"SELECT add1 FROM tbl_source2 WHERE appkey = '" .$main_appkey. "'");
while ($rw_tblsrce2 = mysqli_fetch_assoc($rs_tblsrce2)) {
$arr_tblsrce2[] = $rw_tblsrce2['add1'];
}
//Get count from the Queries.
$cnt_tblsrce1 = mysqli_num_rows($rs_tblsrce1);
$cnt_tblsrce2 = mysqli_num_rows($rs_tblsrce2);
//Check and get who has the largest number between arrays (actually its mysql_num_rows).
$newarr_length = max($cnt_tblsrce1,$cnt_tblsrce2);
//Change the size of each array and put nulls on additional empty rows.
$arr_tblsrce1 = array_pad($arr_tblsrce1,$newarr_length,'Empty');
$arr_tblsrce2 = array_pad($arr_tblsrce2,$newarr_length,'Empty');
}
print_r($arr_tblsrce1);
echo "<br>";
print_r($arr_tblsrce2);
echo "<br>";
mysqli_close($con);
?>
我唯一的问题是如何让这些数组长度相同?查看包含max
和array_pad
这是输出
Array ( [0] => Person 1 [1] => Person 1 [2] => Person 1 [3] => Person 1 [4] => Person 2 )
Array ( [0] => Address 1 [1] => Address 2 [2] => Empty [3] => Empty )
TYSM
答案 0 :(得分:3)
<强>问题:强>
您的代码之所以能够正常工作,是因为它在循环结束时替换了此$arr_tblsrce1
变量的值。让我们说根据您的表格的最后一个值是456
。
这个值只是 1 。
$rs_tblsrce1 = mysqli_query($con,"SELECT name FROM tbl_source1 WHERE appkey = '" .$main_appkey. "'");
,第二个是 0 :
$rs_tblsrce2 = mysqli_query($con,"SELECT add1 FROM tbl_source2 WHERE appkey = '" .$main_appkey. "'");
<强>解决方案:强> 在您的代码中替换此
//Get count from the Queries.
$cnt_tblsrce1 = mysqli_num_rows($rs_tblsrce1);
$cnt_tblsrce2 = mysqli_num_rows($rs_tblsrce2);
用这个
//Get count from the Queries.
$cnt_tblsrce1 = count($arr_tblsrce1);
$cnt_tblsrce2 = count($arr_tblsrce2);
并将此代码放在foreach循环之外
//Get count from the Queries.
$cnt_tblsrce1 = count($arr_tblsrce1);
$cnt_tblsrce2 = count($arr_tblsrce2);
//Check and get who has the largest number between arrays (actually its mysql_num_rows).
$newarr_length = max($cnt_tblsrce1,$cnt_tblsrce2);
//Change the size of each array and put nulls on additional empty rows.
$arr_tblsrce1 = array_pad($arr_tblsrce1,$newarr_length,'Empty');
$arr_tblsrce2 = array_pad($arr_tblsrce2,$newarr_length,'Empty');
以下是整个代码:
require 'conn.php';
//Array Collectors
$arr_tblsrce1 = array();
$arr_tblsrce2 = array();
$arr_tbldest = array();
//Main Query
$rs_tbldest = mysqli_query($con, 'SELECT appkey FROM tbl_destination');
while ($rw_tbldest = mysqli_fetch_assoc($rs_tbldest)) {
$arr_tbldest[] = $rw_tbldest['appkey'];
}
//Do the Following procedures during the loop of Main Query Array
//for ($i=0; $i < count($arr_tbldest); $i++) {
foreach ($arr_tbldest as $main_appkey) {
//Create an array from tbl_source1
$rs_tblsrce1 = mysqli_query($con,"SELECT name FROM tbl_source1 WHERE appkey = '" .$main_appkey. "'");
while ($rw_tblsrce1 = mysqli_fetch_assoc($rs_tblsrce1)) {
$arr_tblsrce1[] = $rw_tblsrce1['name'];
}
//4
//Create an array from tbl_source2
$rs_tblsrce2 = mysqli_query($con,"SELECT add1 FROM tbl_source2 WHERE appkey = '" .$main_appkey. "'");
while ($rw_tblsrce2 = mysqli_fetch_assoc($rs_tblsrce2)) {
$arr_tblsrce2[] = $rw_tblsrce2['add1'];
}
//2
}
//Get count from the Queries.
$cnt_tblsrce1 = count($arr_tblsrce1);
$cnt_tblsrce2 = count($arr_tblsrce2);
//Check and get who has the largest number between arrays (actually its mysql_num_rows).
$newarr_length = max($cnt_tblsrce1,$cnt_tblsrce2);
//Change the size of each array and put nulls on additional empty rows.
$arr_tblsrce1 = array_pad($arr_tblsrce1,$newarr_length,'Empty');
$arr_tblsrce2 = array_pad($arr_tblsrce2,$newarr_length,'Empty');
print_r($arr_tblsrce1);
echo "<br>";
print_r($arr_tblsrce2);
echo "<br>";
mysqli_close($con);
答案 1 :(得分:2)
您应该尝试使用FULL JOIN
查询
尝试如下并适当更改,
$rs_tblsrce12 = mysqli_query($con,"SELECT name,add1 FROM tbl_source1 t1
FULL JOIN tbl_source2 t2 ON t1.appkey = t2.appkey WHERE appkey = '" .$main_appkey. "'");
while ($rw_tblsrce1 = mysqli_fetch_assoc($rs_tblsrce12)) {
$arr_tblsrce1[] = $rw_tblsrce1['name'];
$arr_tblsrce2[] = $rw_tblsrce1['add1'];
}
答案 2 :(得分:0)
我假设两个数组之间没有1到1的映射,为了使它们长度相同,只需在这里稍微改一下
$newarr_length = max($cnt_tblsrce1,$cnt_tblsrce2); // returns bigger array
$newarr_length = count($newarr_length); // get the length
//Change the size of each array and put nulls on additional empty rows.
$arr_tblsrce1 = array_pad($arr_tblsrce1,$newarr_length,'Empty');
$arr_tblsrce2 = array_pad($arr_tblsrce2,$newarr_length,'Empty');
答案 3 :(得分:0)
尝试使用数组填充而不是数组填充
<?php
$a1=array_fill(3,4,"blue");
print_r($a1);
?>
$sizeoffirstsrc = sizeof($cnt_tblsrce1);
$sizeofsecondsrc = sizeof($cnt_tblsrce2);
$newarr_length = max(sizeoffirstsrc,sizeofsecondsrc);
//Change the size of each array and put nulls on additional empty rows.
$arr_tblsrce1 = array_fill($sizeoffirstsrc,$newarr_length,'Empty');
$arr_tblsrce2 = array_fill($sizeofsecondsrc,$newarr_length,'Empty');