簡體   English   中英

php中的`IF語句`array()`函數

[英]php IF statements inside `array()` function

我試圖根據非空的查詢結果設置一個數組。 反過來,我希望以某種方式根據NON Null結果創建排名。

SQL Query返回:

Comp1    Comp2   Comp3   Comp4   Comp5   Comp6   Comp7   Comp8   Comp9
NULL     NULL    226.97   274    NULL    208     189     NULL    198.99

我的PHP:

$COMP1 = $rankresult['Comp1'];
$COMP2 = $rankresult['Comp2'];
$COMP3 = $rankresult['Comp3'];
$COMP4 = $rankresult['Comp4'];
$COMP5 = $rankresult['Comp5'];
$COMP6 = $rankresult['Comp6'];
$COMP7 = $rankresult['Comp7'];
$COMP8 = $rankresult['Comp8'];
$COMP9 = $rankresult['Comp9'];

這不起作用,因為我試圖只放入非空的變量:

$myarray = 'array(
        if(!empty($COMP1)){ 
        $COMP1,}
        if(!empty($COMP2)){ 
        $COMP2,}
        if(!empty($COMP3)){ 
        $COMP3,}
        if(!empty($COMP4)){ 
        $COMP4,}
        if(!empty($COMP5)){ 
        $COMP5,}
        if(!empty($COMP6)){ 
        $COMP6,}
        if(!empty($COMP7)){ 
        $COMP7,}
        if(!empty($COMP8)){ 
        $COMP8,}
        if(!empty($COMP9)){ 
        $COMP9})';

期望的輸出:

$myarray = array(226.97,274,208,189,198.99)

你試過array_filter()嗎?

$result = array_filter($rankresult);

結果:

array(
    "comp3"=>226.97,
    "comp4"=>274,
    "comp6"=>208,
    "comp7"=>189,
    "comp9"=>198.99
)

你有沒有關於使用循環?

如果您具有結果查詢的大小,則可以執行以下操作:

$myarray=new array();
for ($ix=1; $ix<$num_rows; $ix++) {
    if (${COMP.$ix}!=NULL)
        array_push($myarray,${COMP.$ix});
}

更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM