坑爹的php方法1 fputcsv和in_array

以php为主工作语言还不到2年的时间,实在被有些方法坑的不要不要的

http://www.php.net/manual/zh/function.fputcsv.php

int fputcsv ( resource $handle , array $fields [, string $delimiter = ',' [, string $enclosure = '"' ]] )

重点在第3个参数上,默认是双引号

所有人都以为它会给每个输出的内容都会加上"",类似"1","2","3","4"

结果这个方法只有在输出的内容有空格的时候才会加引号  "1 2"," 3",4  这是什么样子的输出

完全无法控制,根本不知道还有别的触发时机没,只能彻底放弃此方法

    public function writeData($aData, $sDelimiter = ',', $sEnclosure = '"') {

        foreach ($aData as $i => $value) {
            $aData[$i] = mb_convert_encoding($value, $this->getToEncoding(), $this->getFromEncoding());
        }

        if (!isset($sEnclosure) || $sEnclosure === '') {
            $sDataString = implode($sDelimiter, $aData) . $this->getEnterCode();

        } else {
            $sDataString = $sEnclosure.implode($sEnclosure.$sDelimiter.$sEnclosure, $aData) .$sEnclosure. $this->getEnterCode();

        }
        $bRet = fwrite($this->oFile, $sDataString);
        return $bRet;
    }

http://php.net/manual/zh/function.in-array.php

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

!!!!!!

一定要把那个$strict 设为true啊,如果用默认的false,会死的很惨的,譬如官方下面举得那个例子

!!!!!!

$array = array(
    'egg' => true,
    'cheese' => false,
    'hair' => 765,
    'goblins' => null,
    'ogres' => 'no ogres allowed in this array'
);

// Loose checking -- return values are in comments

// First three make sense, last four do not

in_array(null, $array); // true
in_array(false, $array); // true
in_array(765, $array); // true
in_array(763, $array); // true
in_array('egg', $array); // true
in_array('hhh', $array); // true
in_array(array(), $array); // true

// Strict checking

in_array(null, $array, true); // true
in_array(false, $array, true); // true
in_array(765, $array, true); // true
in_array(763, $array, true); // false
in_array('egg', $array, true); // false
in_array('hhh', $array, true); // false
in_array(array(), $array, true); // false

猜你喜欢

转载自fighter1945.iteye.com/blog/2314562