PHP:如何将数据库结果(字符串)作为数组(使用explode)添加到序列

时间:2016-07-17 15:37:55

标签: php mysql arrays select null

我想生成一个多维数组,如下所示:

$arrayTwo = [
[ 'This','array','needs','to','be','filled'],
['This','cat','needs','to','be','filled'],
[ 'This','cat','needs','to','get','eaten'],
[ 'This','array','needs','to','get','eaten'],
[ 'This','array','needs','to','count','sheep']
];

我创建的代码使用数据库搜索的最高结果填充空白,基于长度,然后通过google-results,然后bing-results,然后是yahoo-results进行过滤。但是,这意味着如果我有一个包含两个连续NULL成员的多个实例的数组,那么每次都会用相同的单词对填充它们。首次将$arrayOne输入$arrayTwo时这不是问题,因为它是一个有效的响应,但如果我创建一个循环,那么我希望$arrayOne的下一个表达式包含在google- / bing- / yahoo-results部分中具有次高值的单词组的选择。

数据库:

|column 1   |length |google-results |bing-results   |yahoo-results  |
|array      |1      |1213253        |3456365        |253645         |
|be filled  |2      |5463656        |4255665        |424535         |
|cat        |1      |4564747        |9678744        |345636         |
|get eaten  |2      |4543636        |5356456        |424253         |
|count sheep|2      |4355665        |5645647        |476578         |
|umbrella   |1      |6973435        |5356356        |346476         |

HTML

$arrayOne = [0=> 'This',1=> ,2=> 'needs',3=> 'to',4=> ,5=> ];
$arrayTwo = [];

array_unshift($arrayOne, 'Suggestion:');

for($i=0;$i<count($arrayOne);$i++) {
    if($arrayOne[$i] == NULL) {
        if($arrayOne[$i-1] != NULL) {
            $counter = 1;
            for($x=$i+1;$x<count($arrayOne);$x++) {

                if($arrayOne[$x] == NULL && $arrayOne[$x-1] == NULL) {

                    $counter++;
                }

                elseif($arrayOne[$x] != NULL && $arrayOne[$x-1] == NULL) {

                    $queryBlank = "SELECT * FROM table_name WHERE length = ".$counter." ORDER BY `google-results` DESC, `bing-results` DESC, `yahoo-results` DESC";
                    $resultBlank = mysqli_query($conn,$queryBlank);

                    if($row = mysqli_fetch_assoc($resultBlank)) {

                        $blank = explode(' ', $row['column 1']);

                        foreach($blank as $key => $value) {

                            $arrayOne[$i+$key] = $value;
                        }
                    }
                }
            }
            array_push($arrayTwo, $arrayOne);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我不知道我的结果与你的想法完全不同

但我尝试了最好的xD

$arrayOne = [0=> 'This',1=> '',2=> 'needs',3=> 'to',4=> '',5=> ''];
$arrayTwo = [];
$arrayThree = [];

for($i=0;$i<count($arrayOne);$i++) {
    if($arrayOne[$i] == NULL) {
        if(isset($arrayOne[$i+1]) && $arrayOne[$i+1] != NULL){
            $counter = 1;
            $queryBlank = "SELECT * FROM table_name WHERE length = ".$counter." ORDER BY `google-results` ASC, `bing-results` ASC, `yahoo-results` ASC";
            $resultBlank = mysqli_query($conn,$queryBlank);
            while($row = mysqli_fetch_array($resultBlank)) {
                // echo $row['column 1'];
                $blank = explode(' ', $row['column 1']);
                $arrayTemp = array();
                foreach($blank as $key => $value) {
                    $arrayTemp[$i+$key] = $value;
                }
                array_push($arrayTwo, $arrayTemp);
            }
        }elseif(isset($arrayOne[$i+1]) && $arrayOne[$i+1] == NULL){
            // echo $i;
            $counter = 2;
            $queryBlank = "SELECT * FROM table_name WHERE length = ".$counter." ORDER BY `google-results` ASC, `bing-results` ASC, `yahoo-results` ASC";
            $resultBlank = mysqli_query($conn,$queryBlank);
            while($row = mysqli_fetch_array($resultBlank)) {
                // echo $row['column 1'];
                $blank = explode(' ', $row['column 1']);
                $arrayTemp = array();
                foreach($blank as $key => $value) {
                    $arrayTemp[$i+$key] = $value;
                }
                array_push($arrayThree, $arrayTemp);
            }
        }
    }
}
foreach ($arrayTwo as $key => $value) {
    $newArray = array_replace($arrayOne,$value);
    foreach ($arrayThree as $key2 => $value2) {
        print_r(array_replace($newArray,$value2));
        echo '<br>';
    }

}

我的结果

enter image description here

为什么count sheep是最佳匹配。请重新检查您的数据库5645647&gt; be filled5463656

。{

我不确定,但您可以用自己的想法设置ASCDESC