php if语句问题与爆炸数组值

时间:2016-08-14 03:49:12

标签: php arrays javascriptserializer

我的if ($arr[$key][0] == 'th') //无效

我一直在尝试将我的数组值转换为字符串,但仍然是我的if语句不正确,请帮助!!!

代码:

$preparedstring = "
pb,List information,ca;
'th',Name,ca;
th,Surname,ca;
th,ID,ca;
th,Gender,ca;
th,Gender,ca;
pb,personal details,ca;
fl,Name,la;
fl,Surname,la;
fl,ID,la;
fl,Gender,la
";

$columncount = 0;
$firstindex = 0;
$lastindex = 0;
$functionvalue = "";

$outerARR = explode(";", $preparedstring);
$arr = array();

foreach ($outerARR as $arrvalue){


    $innerarr = explode(",", $arrvalue);
    $arr[] = $innerarr;

}
print_r($arr);

unset($arrvalue);
//Read the array
foreach ($arr as $key => $arrvalue1){
    $functionvalue = $arr[$key][0];

    echo $functionvalue;

    //Get column key functions
        if ($arr[$key][0] == 'th'){
            $firstindex++;
            echo "<p>".$a."</p><br/>";
        }    
        elseif ( $firstindex > 0 )
        {
                $lastindex = $key - 1;
        }   

    foreach ($arrvalue1 as $arrvalue2){

           }
    $a++;
}

echo "<p>".$arr[0][0].".</p><br/> First Index : ".$firstindex. " Last Index : ".$lastindex. "---".$functionvalue;

输出:

Array ( [0] => Array ( [0] => pb [1] => List information [2] => ca ) [1] => Array ( [0] => 'th' [1] => Name [2] => ca ) [2] => Array ( [0] => th [1] => Surname [2] => ca ) [3] => Array ( [0] => th [1] => ID [2] => ca ) [4] => Array ( [0] => th [1] => Gender [2] => ca ) [5] => Array ( [0] => th [1] => Gender [2] => ca ) [6] => Array ( [0] => pb [1] => personal details [2] => ca ) [7] => Array ( [0] => fl [1] => Name [2] => la ) [8] => Array ( [0] => fl [1] => Surname [2] => la ) [9] => Array ( [0] => fl [1] => ID [2] => la ) [10] => Array ( [0] => fl [1] => Gender [2] => la ) ) 

pb 'th' th th th th pb fl fl fl fl

pb.

First Index : 0 Last Index : 0--- fl 

这是输出,因为你可以看到我的if语句是假的

1 个答案:

答案 0 :(得分:0)

您需要从字符串中删除所有换行符,然后执行操作:

$outerARR = explode(";", preg_replace('~[\r\n]+~', '', $preparedstring));