doujing6436 2017-12-06 20:54
浏览 55

PHP foreach循环数据类型的复杂性和值检索

I am trying to pull data out of a foreach loop but I'm running into a strange phenomenon that I haven't encountered yet.

The first row of the CSV looks like this:

Part Number,Description,List Price

Then Data so on and so forth.

What I have:

ini_set('auto_detect_line_endings', TRUE);
if (($openItems = fopen($uploadFile, 'r')) !== FALSE) {
   foreach (fgetcsv($openItems) as $key => $value) {
        $value = preg_replace('/\h+/', '', strtolower($value));
        echo $value;
        echo ' = ' . $key . " ";
        switch ($value) {
            case "partnumber":
                $pn = $key;
                break;
            case "description":
                $des = $key;
                break;
            case "listprice":
                $lp = $key;
                break;
        }
    }
    print_r("
pn: " . $pn . " des: " . $des . " lp: " . $lp);
}

Print:

partnumber = 0 description = 1 listprice = 2 
pn:  des: 1 lp: 2

As you can see the first column is falsey so it is not assigned the proper value.

Alternative Print:

test = 0 partnumber = 1 description = 2 listprice = 3 
pn: 1 des: 2 lp: 3

I also tried an exercise after $key was echoed that checked:

if($value == 0) {
    echo " I'm 0 ";
}

Print:

partnumber = 0  I'm 0. description = 1  I'm 0. listprice = 2  I'm 0. 
pn:  des: 1 lp: 2

But if you check for numeracy it comes back false:

if(is_numeric($value)) {
    echo " I'm a number. ";
}

Print is unchanged:

partnumber = 0 description = 1 listprice = 2 
pn:  des: 1 lp: 2

That means it is 0 in a NULL or falsey sense right?

if(!$value) {
    echo " I'm False or Null. ";
}

But Print is unchanged:

partnumber = 0 description = 1 listprice = 2 
pn:  des: 1 lp: 2

Type check:

echo "I'm of Type ". gettype($value) . ". ";

returns:

partnumber = 0 I'm of Type string. description = 1 I'm of Type string. listprice = 2 I'm of Type string. 

Side Note:

case "partnumber":
    $pn = $key;
    break;

is never accessed

case "partnumber":
    $pn = $key;
    echo "I Worked!";
    break;

This returns with out any result.

Any, Suggestions?

This Scinario is thanks to @azjezz

$csv = [];
ini_set('auto_detect_line_endings', TRUE);
if (($openItems = fopen($uploadFile, 'r')) !== FALSE) {
    foreach (fgetcsv($openItems) as $key => $value) {
        $value = preg_replace('/\h+/', '', strtolower($value));
        $csv[$value] = (Int) $key;
        echo $value;
        echo ' = ' . $key . " ";
        switch ($csv[$value]) {
            case "partnumber":
                $pn = $key;
                break;
            case "description":
                $des = $key;
                break;
            case "listprice":
                $lp = $key;
                break;
        }
    }
    print_r("
pn: " . $pn . " des: " . $des . " lp: " . $lp);
}

Results in:

partnumber = 0 description = 1 listprice = 2 
pn: 0 des:  lp: 

Another weird scinario:

    switch ((int) $value) {
        case "partnumber":
            $pn = $key;
            break;
        case "description":
            $des = $key;
            break;
        case "listprice":
            $lp = $key;
            break;
    }

Returns:

partnumber = 0 description = 1 listprice = 2 
pn: 2 des:  lp: 
  • 写回答

1条回答 默认 最新

  • douduidui1046 2017-12-06 21:04
    关注

    Convert all values to integer then use $csv array.

    $csv = [];
    foreach (fgetcsv($openItems) as $key => $value) {
      $csv[$value] = (Int) $key;
    }
    
    var_dump($csv);
    

    Note that == and === are not the same!

    <?php 
    
    $a = 0;
    $b = "0";
    
    // == is the Equal Comparison Operator
    //if $a is equal to $b after type juggling.
    if($a == $b) {
      echo "YES ITS TRUE";
    }
    
    // === is the Identical Comparison Operator
    //  if $a is equal to $b, and they are of the same type. 
    if($a === $b) {
      echo "Is it ?";
    } else {
      echo "well its not";
    }
    

    you can read more about comparison operators here : http://php.net/manual/en/language.operators.comparison.php

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化