简体   繁体   English

是否可以在不使用IF的情况下用A或B填充数组a?

[英]Can i fill Array a with A or B without using IF?

EDIT: 编辑:

Found my solution :D 找到我的解决方案: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
                         );
}

Situation was this: 情况是这样的:

I have following code... 我有以下代码...

// 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]
                             );
}

is there a way to let php choose what value is passed when other value is empty ? 有没有一种方法可以让php选择在其他值为空时传递什么值?

eg 例如

// 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
                             );
}

it doesn't work like this :) saw some like this in javascript 它不像这样工作:)在javascript中看到了这样的内容

or do i have to use IF all the time ? 还是我必须一直使用IF i don't want to use a short writen IF like 我不想使用简短的IF

( $foo ? $foo : 0 )

eather. eather。 trying to avoid writing my var two times 试图避免两次写我的var

output atm is: 输出atm为:

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

and i want this (ONLY on fields i've choosen): 我想要这个(仅在我选择的字段上):

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

Since you want to do it without having if statements all over the place, here's a way with only one if: 由于您要这样做而不必到处都有if语句,因此这是一种仅使用if的方法:

// 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]
    );
}

Still not quite at your no-if's goal of course. 当然,仍然不能完全达到您的“如果没有”目标。

.

[EDIT] [编辑]

If the something[ ] array will only be numbers you could probably just do: 如果something []数组仅是数字,则可能可以执行以下操作:

// 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])
    );
}
$tmp[4] => isset( $something[12] )? something[12] : 0
//         ^^^^^^^^^^^^^^^^^^^^^^   ^^^^^^^^^^^^^   ^
//                 condition           when true    else   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM