我想做
<?php
$str = "I want to access 2nd or 3rd index in one line";
echo explode(" ",$str)[2];
?>
我们可以使用
轻松访问第一个索引stristr($str," ",true); //For php version >= 5.3
或
$foo = array_shift(explode(':', $foo));
或
list($str) = explode(" ", $str);
BUT
如何在一行中访问特定索引[1],[2]或[3] ???
答案 0 :(得分:2)
<强> 5.4 + 强>
<?php echo explode(" ","I want to access 2nd or 3rd index in one line")[2]; ?>
答案 1 :(得分:0)
第二次
strtok($string, " "); echo strtok(" ");
或第三
strtok($string, " "); strtok(" "); echo strtok(" ");
答案 2 :(得分:-1)
尝试这样获得特定的索引值
<?php
$str = "Iam want to access 2nd or 3rd index in one line";
$val = explode(" ",$str);
echo $val[3];
?>