PHP explode在一行中获取特定的索引值

时间:2013-06-13 07:09:56

标签: php arrays line explode

我想做

<?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] ???

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];
?>