23

Is there a way to explode a string into variables e.g

some_function($min, $max, "3, 20");

such that $min is assigned the value 3 and $max is assigned the value 20.

I know I can simply use

$data = explode("3, 20");

just wondering if there is another way.

3 Answers 3

49

PHP's language construct list() can perform multiple assignments to variables (or even other array keys) by assigning an array.

list($min, $max) = explode(",", "3,20");

However, you would still need to apply a trim() to your variables since the $max value would have a leading space, or replace explode() with preg_split('/\s*,\s*/', $string) to split it on commas and surrounding whitespace.

Note: Use caution with list() to be sure that the array you're assigning contains the same number of elements as list() has variables.

In PHP 5.x, when assigning a value directly to another array, as an element of that array, list() values are assigned from right to left in PHP 5.x, not left to right. In other words, you'll end up with array that is populated backwards (last value, first).

https://www.php.net/manual/en/migration70.incompatible.php

In PHP 7.x list() arguments are assigned from left to right, when assigning elements directly to an array. In other words, you'll end up with the first value as the first element in the recipient array.

<?php
    list($a[], $a[], $a[]) = [1, 2, 3];
    var_dump($a);
?>

PHP Manual

PHP 5.X Last value gets the first element position, but the recipient must an array (in this case $a, is the array)!

array(3) {
  [0]=>
  int(3)
  [1]=>
  int(2)
  [2]=>
  int(1)
}

PHP 7.x First value becomes the first array element.

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

PHP Manual

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

8 Comments

Technically that is the language construct of list(). But +1.
@DaveRandom I didn't say list() was a function, I said that's the function of list(). Subtle, but I'll clarify anyway :)
Thanx.....i will read up on the list function, As for the note, i will use the limit parameter on the explode function. am guessing that is its function right?
@kayfun Yes that's the purpose of explode()'s limit.
Why not just set the delimiter to ", " and skip the whole trim() thing
|
10

Since PHP 7.1 there is a new feature called

Symmetric array destructuring: The shorthand array syntax ([]) may now be used to destructure arrays for assignments (including within foreach), as an alternative to the existing list() syntax, which is still supported.

That means that you can do now:

[$one, $two] = explode(";", "one;two");
echo $one; // one
echo $two; // two

As an alternative to list(), I find it myself a very elegant and readable solution that arrived with 7.1+ ... it is the same, but better.

Note: As the quote indicates, you can use the shorthand array syntax ([]) to assign variables in foreach declarations, but that's not related to the question, so check it out!

Documentation: https://www.php.net/manual/en/migration71.new-features.php

Comments

1

This case is the perfect reason to use sscanf(). Different from explode(), this string parsing function allows substrings to be parsed into an array or individual variables WITH the ability to cast numeric values as integer-type (%d) or float-type (%f). Even whitespaces are forgiven. Demo

sscanf('3, 20', '%d,%d', $min, $max);
var_dump($min, $max);

Output:

int(3)
int(20)

Comments

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.