發現我的解決方案: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
)
THX所有的海報,我發現我的解決方案由我自己:d – Dwza