0

For example:

echo explode('@',$var); //but only echoing element 1 or 2 of the array?

Instead of:

$variable = explode('@',$var);
echo $variable[0];

Thank you.

OBV
  • 1,169
  • 1
  • 12
  • 25

5 Answers5

7

On PHP versions that support array dereferencing, you can use the following syntax:

echo explode('@', $var)[0];

If not, you can use list():

list($foo) = explode('@', $var);

The above statement will assign the first value of the exploded array in the variable $foo.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
6

Since PHP 5.4 you can write:

echo explode('@', $var)[0];

In earlier versions of PHP you can only achieve the behaviour with tricks:

echo current(explode('@', $var)); // get [0]
echo next(explode('@', $var));    // get [1]

Retreiving an element at an arbitrary position is not possible without a temporary variable.


Here is a simple function that can tidy your code if you don't want to use a variable every time:

function GetAt($arr, $key = 0)
{
    return $arr[$key];
}

Call like:

echo GetAt(explode('@', $var));    // get [0]
echo GetAt(explode('@', $var), 1); // get [1]
Cobra_Fast
  • 15,671
  • 8
  • 57
  • 102
  • 3
    Technically it was possible, you could do `echo current(explode('@', $var));` – SubjectCurio Oct 16 '13 at 16:09
  • do those same tricks work in <5.4 aswell? I am looking for a general solution and not worry about the compatability issues – OBV Oct 16 '13 at 16:15
  • @OBV Yes, this behaviour should still be functional in 5.4 and later versions. The safest bet is probably a temporary variable though. – Cobra_Fast Oct 16 '13 at 16:16
  • @OBV They do work, although I'm not sure how far back they'd work. Regardless, I wouldn't recommend using them as they disregard PHP Strict Standard rules. There's no real reason to not just assign it to a variable, 1 line of code is nothing and it's just considerably better practice to do so. – SubjectCurio Oct 16 '13 at 16:18
  • I tested: $var = "@hello@world"; echo current(explode('@', $var)); echo next(explode('@', $var)); and only "hello" appeared, any ideas? – OBV Oct 16 '13 at 16:25
  • 1
    @OBV There is nothing in front of your first `@`, so `current()` returns an empty string. – Cobra_Fast Oct 16 '13 at 16:26
  • 1
    Lets just hope, that no one will take most bottom example too literally. Because it will be completely ineffective (: – BlitZ Oct 16 '13 at 16:31
1

Previous to PHP 5.4 you could do this:

echo array_shift(explode('@', $var));

That would echo the first element of the array created by explode. But it is doing so without error checking the output of explode, which is not ideal.

SamA
  • 587
  • 3
  • 6
1

list can be used like this as well:

list($first) = explode('@', $var);
list(,$second) = explode('@', $var);
list(,,$third) = explode('@', $var);
Halcyon
  • 57,230
  • 10
  • 89
  • 128
0

Just use reset like this

echo reset(explode('@', $var));
JoDev
  • 6,633
  • 1
  • 22
  • 37
  • I made a test before to post... It works find for me! `exit(reset(explode('-', 'it-is-my-test')));` – JoDev Oct 16 '13 at 16:08