4

I want to do

<?php 
$str = "I want to access 2nd or 3rd index in one line";
echo explode(" ",$str)[2];
?>

We can access first index easily using

stristr($str," ",true);  //For php version >= 5.3

or

$foo = array_shift(explode(':', $foo));

or

list($str) = explode(" ", $str);

BUT

HOW TO ACCESS SPECIFIC INDEX [1],[2] OR [3] IN ONE LINE???

Wasim A.
  • 9,660
  • 22
  • 90
  • 120

3 Answers3

3

5.4+

<?php echo explode(" ","I want to access 2nd or 3rd index in one line")[2]; ?>
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
0

for second

strtok($string, " "); echo strtok(" ");

or third

strtok($string, " "); strtok(" "); echo strtok(" ");
Orangepill
  • 24,500
  • 3
  • 42
  • 63
-1

Try like this to get specific index value

<?php 
  $str = "Iam want to access 2nd or 3rd index in one line";
  $val = explode(" ",$str);
  echo $val[3];
?>
Arunprabu
  • 11
  • 3