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.

7
  • Can the downvoter explain himself ? Commented Oct 16, 2013 at 16:06
  • Explode converts string to array. How can you access array element while it is a string ? Commented Oct 16, 2013 at 16:07
  • I did try to solve the problem, but to no avail. I have about 6 months experience with PHP, and nothing has stuck out in what i have read in how to solve this. Commented Oct 16, 2013 at 16:09
  • possible duplicate of PHP: Access Array Value on the Fly Commented Oct 16, 2013 at 16:18
  • 1
    @OBV: Here's a demo using list() that doesn't use temporary variables or additional function calls -- eval.in/54886 Commented Oct 16, 2013 at 16:47

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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]

7 Comments

Technically it was possible, you could do echo current(explode('@', $var));
do those same tricks work in <5.4 aswell? I am looking for a general solution and not worry about the compatability issues
@OBV Yes, this behaviour should still be functional in 5.4 and later versions. The safest bet is probably a temporary variable though.
@OBV There is nothing in front of your first @, so current() returns an empty string.
Lets just hope, that no one will take most bottom example too literally. Because it will be completely ineffective (:
|
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.

Comments

1

list can be used like this as well:

list($first) = explode('@', $var);
list(,$second) = explode('@', $var);
list(,,$third) = explode('@', $var);

Comments

0

Just use reset like this

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

1 Comment

I made a test before to post... It works find for me! exit(reset(explode('-', 'it-is-my-test')));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.