2014-02-07 31 views
0

編輯:我可以用A或B填充數組a而不使用IF嗎?

發現我的解決方案:d

while($something = fgets(...)) 
{ 
$var[$counter++] = array($tmp[0] => $something[10)], 
          $tmp[1] => str_pad($something[4],1,0) // 0 if empty 
          $tmp[2] => str_pad($something[7],1,0) // 0 if empty 
          $tmp[3] => str_pad($something[1],1,0), // 0 if empty 
          $tmp[4] => str_pad($something[12],1,"x") // x if empty 
         ); 
} 

情況是這樣的:

我有以下代碼...

// reading something from file.... 
$var; 
$tmp  = explode(",","aa,bb,cc,dd,ee"); 
$counter = 0; 
while($something = fgets(...)) 
{ 
    $var[$counter++] = array($tmp[0] => $something[10], 
           $tmp[1] => $something[4], 
           $tmp[2] => $something[7], 
           $tmp[3] => $something[1], 
           $tmp[4] => $something[12] 
          ); 
} 

是有辦法讓php選擇當其他值爲空時傳遞的值?

例如

// reading something from file.... 
$var; 
$tmp  = explode(",","aa,bb,cc,dd,ee"); 
$counter = 0; 
while($something = fgets(...)) 
{ 
    $var[$counter++] = array($tmp[0] => $something[10], 
           $tmp[1] => $something[4] || 0, 
           $tmp[2] => $something[7] || 0, 
           $tmp[3] => $something[1] || 0, 
           $tmp[4] => $something[12] || 0 
          ); 
} 

它不喜歡這個工作,:)看到了一些類似這樣的JavaScript

或做我必須使用IF所有的時間?我不想使用短writen IF

($foo ? $foo : 0) 

eather。儘量避免寫我無功兩次

輸出ATM是:

Array 
(
    [0] => Array 
     (
      [aa] => 
      [bb] => "some" 
      [cc] => 
      [cc] => "some" 
      [ee] => 
     ) 

,我想這(僅在領域我選擇):

Array 
(
    [0] => Array 
     (
      [aa] =>   //<=== This can stay empty 
      [bb] => "some" 
      [cc] => 0 
      [cc] => "some" 
      [ee] => 0 
     ) 
+0

THX所有的海報,我發現我的解決方案由我自己:d – Dwza

回答

0
$tmp[4] => isset($something[12])? something[12] : 0 
//   ^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ ^
//     condition   when true else 
+0

這個ISN 't我在找eather ...這是一個簡短的表格,如果:)我不想有1 var var doubble :)它只是一個很好的東西 – Dwza

+0

但它不會說「if」;) –

+0

true哈哈哈:)你甚至不必寫isset它仍然會工作 – Dwza

1

既然你想做到這一點,如果沒有如果陳述遍佈各地,這裏是一種方式,只有一個如果:

// reading something from file.... 
$var; 
$tmp  = explode(",","aa,bb,cc,dd,ee"); 
$counter = 0; 
while($something = fgets(...)) 
    { 
    //add in a while loop to find empty somethings: 

    //---------------------------------- 
    $somethingCount = count($something); 
    $at = 0; 
    while($at < $somethingCount) 
    { 
     if(empty($something[$at])) 
     { 
      $something[$at] = 0; 
     } 
     $at++; 
    } 
    // --------------------------------- 


    //now you can do your own thing without caring about the empties: 

    $var[$counter++] = array( 
     $tmp[0] => $something[10], 
     $tmp[1] => $something[4], 
     $tmp[2] => $something[7], 
     $tmp[3] => $something[1], 
     $tmp[4] => $something[12] 
    ); 
} 

當然,還不完全在你的無目標的目標。

[編輯]

如果東西[]數組只能是數字,你很可能只是做:

// reading something from file.... 
$var; 
$tmp  = explode(",","aa,bb,cc,dd,ee"); 
$counter = 0; 
while($something = fgets(...)) 
    { 


    $var[$counter++] = array( 
     $tmp[0] => max(0, $something[10]), 
     $tmp[1] => max(0, $something[4]), 
     $tmp[2] => max(0, $something[7]), 
     $tmp[3] => max(0, $something[1]), 
     $tmp[4] => max(0, $something[12]) 
    ); 
} 
+0

謝謝你的帖子,不...有數字和字符串,而同時公關問題仍然存在問題,如果它們爲空,我不想將所有語句設置爲零。 :)猜不對只是寫了一個像** prepareEntry($ string,$ returnvalue = 0)的函數** – Dwza

+0

通過寫你的評論和鍵入函數我有一個想法,它適用於我的情況:D看我的編輯頂端。無論如何 – Dwza

相關問題