使用mysql动态填充php中的数组

时间:2017-12-05 15:17:59

标签: php mysql arrays matrix

我有数据库,我想在PHP中创建矩阵或可能是2d数组,其中第一列来自第一个查询,然后我再次循环另一个查询。我得到它但是当涉及到矩阵时,我想要从第一列填充第一列,并且该行中的其余列应该填充,具体取决于该特定查询的数据。看完代码后你可能会更好地理解

 <!DOCTYPE html>

<html>

<?php
require ('connect.php');
$pro_name = $_POST['project'];
$sheet_type = $_POST['sheet_set'];    
echo $pro_name ."-";
echo $sheet_type . "<br/>";

?>

<?php
$doc_num_data = array();
$qry_doc_num = "SELECT DISTINCT document_number FROM `$pro_name`.Instruments";
$result_doc_num = mysqli_query($connection, $qry_doc_num) or die(mysqli_error($connection));

while($doc_num = mysqli_fetch_row($result_doc_num)){

    if($doc_num[0] != ''){
        $qry_ins = "SELECT CONCAT(tag_letters,'-',tag_numbers) FROM `$pro_name`.Instruments WHERE document_number = '$doc_num[0]'";
        $result_qry_ins = mysqli_query($connection, $qry_ins) or die(mysqli_error($connection));
     //   array_push($doc_num_data, $doc_num);
       // $doc_num_data = array();
        while($ins_doc = mysqli_fetch_row($result_qry_ins)){
            $instruments_tag = $ins_doc[0];
            array_push($doc_num_data, $doc_num);
        }
    }
}
echo "<pre>";
print_r($doc_num_data);
echo "</pre>";
?>

</html>

我想要这样的东西

[doc1] [ins1] [ins2] []
[doc2] [ins1] []     []
[doc3] [ins1] [ins2] [ins3]
[doc4] [ins1] [ins2] []

这就是我得到的结果基本上每个元素都保存在不同的数组

Array (
 [0] => Array
     (
         [0] => test1
     )

 [1] => Array
     (
         [0] => PIT-100
     )

 [2] => Array
     (
         [0] => test1
     )

 [3] => Array
     (
         [0] => PIT-330
     )

 [4] => Array
     (
         [0] => PIT-330
     )

 [5] => Array
     (
         [0] => PIT-330
     )

 [6] => Array
     (
         [0] => test2
     )

 [7] => Array
     (
         [0] => PIT-300
     )

 [8] => Array
     (
         [0] => PIT-300
     )

 [9] => Array
     (
         [0] => PIT-300
     )

 [10] => Array
     (
         [0] => PIT-300
     )

 [11] => Array
     (
         [0] => PIT-300
     )

2 个答案:

答案 0 :(得分:0)

阵 (     [0] =&gt;排列         (             [0] =&gt; TEST1             [1] =&gt; PIT-100             [2] =&gt; PIT-330         )

[1] => Array
    (
        [0] => test2
        [1] => PIT-300
    )

[2] => Array
    (
        [0] => test3
        [1] => TIT-100
    )

[3] => Array
    (
        [0] => test5
        [1] => TIT-200
        [2] => TIT-900
        [3] => PIT-000
    )

[4] => Array
    (
        [0] => test4
        [1] => TIT-1000
    )

[5] => Array
    (
        [0] => test6
        [1] => TIT-999
    )

使用此代码

<?php
$doc_num_data = array();
$qry_doc_num = "SELECT DISTINCT document_number FROM `$pro_name`.Instruments";
$result_doc_num = mysqli_query($connection, $qry_doc_num) or die(mysqli_error($connection));
$i=0;
while($doc_num = mysqli_fetch_row($result_doc_num)){
    $j=1;
    if($doc_num[0] != ''){
        $qry_ins = "SELECT CONCAT(tag_letters,'-',tag_numbers) FROM `$pro_name`.Instruments WHERE document_number = '$doc_num[0]'";
        $result_qry_ins = mysqli_query($connection, $qry_ins) or die(mysqli_error($connection));
        //echo  $doc_num[0];
        $doc_num_data[$i][0] = $doc_num[0];
        //echo $doc_num_data[$i];
       // $doc_num_data = array();
        while($ins_doc = mysqli_fetch_row($result_qry_ins)){
            $instruments_tag = $ins_doc[0];
            $doc_num_data[$i][$j]= $instruments_tag;
            $j=$j+1;
        }
    }
    $i= $i+1;
}

答案 1 :(得分:0)

您遇到的问题是使用array_push()。所有这一切(如你所见)是将值堆叠到LIST中,而不是散列/多维数组。

您想要的是该文档ID引用的内容列表,对吧?

试试这个:

if($instruments_tag != null) array_push($doc_num_data[$doc_num], $instruments_tag);

我认为这是你正在寻找的,或者无论如何都要接近它,并且它最终不会添加那个空值。如果需要,请删除if。